944,083 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4425
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 30th, 2007
0

Returning local array

Expand Post »
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?
Last edited by laconstantine; May 30th, 2007 at 5:04 pm.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 30th, 2007
-1

Re: Returning local array

This is c++, why aren't you using std::strings for starters?
Last edited by iamthwee; May 30th, 2007 at 4:56 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 30th, 2007
0

Re: Returning local array

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 30th, 2007
0

Re: Returning local array

when you say garbage you mean the values that is in the memory location that returned in second example?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 30th, 2007
0

Re: Returning local array

Yes.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 30th, 2007
0

Re: Returning local array

>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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
May 31st, 2007
0

Re: Returning local array

Click to Expand / Collapse  Quote originally posted by Narue ...
Yes.
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??
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 31st, 2007
0

Re: Returning local array

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 31st, 2007
0

Re: Returning local array

Quote ...
Your compiler didnt use the memory immediately
What do you mean?

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 31st, 2007
0

Re: Returning local array

I mean that when you create a local variable, the memory for it is reserved to that variable. When you destroy that local variable, the compiler can use that memory for anything it wants. Basically, the memory is no longer yours to access.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: hex converting
Next Thread in C++ Forum Timeline: Windows api





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC