Returning local array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Returning local array

 
0
  #1
May 30th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Returning local array

 
-1
  #2
May 30th, 2007
This is c++, why aren't you using std::strings for starters?
Last edited by iamthwee; May 30th, 2007 at 4:56 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Returning local array

 
0
  #3
May 30th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Returning local array

 
0
  #4
May 30th, 2007
when you say garbage you mean the values that is in the memory location that returned in second example?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Returning local array

 
0
  #5
May 30th, 2007
Yes.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Returning local array

 
0
  #6
May 30th, 2007
>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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Returning local array

 
0
  #7
May 31st, 2007
Originally Posted by Narue View Post
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??
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Returning local array

 
0
  #8
May 31st, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Returning local array

 
0
  #9
May 31st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Returning local array

 
0
  #10
May 31st, 2007
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC