| | |
Returning local array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2007
Posts: 69
Reputation:
Solved Threads: 1
Hi Everyone,
I have the following function,
char* sample1()
{
char *p = "Israel";
return(p);
}
//in this case the memory storing india is not destroyed at the end
of the function, indicating that it wasn't stored in the stack meant
for the function call.
However, the following function causes unexpected behavior as
expected ;-)
char* sample2()
{
char p[] = "Israel";
return(p);
}
Is it because that the value that in the address that returned by sample2 is still the same i mean its not magicly reseted to zero?
I have the following function,
char* sample1()
{
char *p = "Israel";
return(p);
}
//in this case the memory storing india is not destroyed at the end
of the function, indicating that it wasn't stored in the stack meant
for the function call.
However, the following function causes unexpected behavior as
expected ;-)
char* sample2()
{
char p[] = "Israel";
return(p);
}
Is it because that the value that in the address that returned by sample2 is still the same i mean its not magicly reseted to zero?
Last edited by laconstantine; May 30th, 2007 at 5:04 pm.
>char *p = "india";
This is a pointer to a string literal. When you return p, you're returning the address of the string literal. A string literal has a lifetime beyond the function, so the memory isn't reclaimed and you don't get garbage. This is perfectly legal.
>char p[] = "india";
This is a string literal copied into an array. When you return p, you're returning the address of a local variable. A local variable is destroyed when the function returns, so you get garbage. This results in undefined behavior.
This is a pointer to a string literal. When you return p, you're returning the address of the string literal. A string literal has a lifetime beyond the function, so the memory isn't reclaimed and you don't get garbage. This is perfectly legal.
>char p[] = "india";
This is a string literal copied into an array. When you return p, you're returning the address of a local variable. A local variable is destroyed when the function returns, so you get garbage. This results in undefined behavior.
I'm here to prove you wrong.
>char *p = "india";p here points to literal. It is more like
const char* p = "india". Here it is const data, non-const pointer. Hence you can do something like ++p, --p and get away with it, while trying to manipulate the value won't work. > char[] p = "india"p here is a constant pointer pointing to a non-constant data. Here it is non-const data and constant pointer. Hence you can do something like
p[0] = 't' but trying to do ++p or --p won't work. Last edited by ~s.o.s~; May 30th, 2007 at 10:35 pm.
I don't accept change; I don't deserve to live.
•
•
Join Date: May 2007
Posts: 69
Reputation:
Solved Threads: 1
So if i do this
char* sample2()
{
char p[] = "Israel";
return(p);
}
the return value is an address of some memory block (garbage).
But now its about the compiler:
Some compilers can leave this values and they still will be the same(Its with VC++ 2005)
And when we will try to use it the values still will be "Israel".
And some compilers are reseting the values to NULL and when we are trying to use it we are geting NULL values.
Yes?
If its yes i have no more questions.
And sorry about me and my questions.
So is it correct??
char* sample2()
{
char p[] = "Israel";
return(p);
}
the return value is an address of some memory block (garbage).
But now its about the compiler:
Some compilers can leave this values and they still will be the same(Its with VC++ 2005)
And when we will try to use it the values still will be "Israel".
And some compilers are reseting the values to NULL and when we are trying to use it we are geting NULL values.
Yes?
If its yes i have no more questions.
And sorry about me and my questions.
So is it correct??
>Yes?
No. Undefined behavior doesn't mean the values will stay the same or be nulled out. It means that the values could become anything at any time after execution leaves the function. Just because your compiler didn't use the memory immediately doesn't mean the same thing will happen on another run, even with the same compiler.
No. Undefined behavior doesn't mean the values will stay the same or be nulled out. It means that the values could become anything at any time after execution leaves the function. Just because your compiler didn't use the memory immediately doesn't mean the same thing will happen on another run, even with the same compiler.
I'm here to prove you wrong.
•
•
Join Date: May 2007
Posts: 69
Reputation:
Solved Threads: 1
•
•
•
•
Your compiler didnt use the memory immediately
You mean that this memory that returned can be used with other program variables or over variables from my program, becuase the function end and the objects are destroyed?
there is no other way the compiler can just magicly use this memory.
Last edited by laconstantine; May 31st, 2007 at 10:25 am.
![]() |
Other Threads in the C++ Forum
- Previous Thread: hex converting
- Next Thread: Windows api
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






