Problem with Returning Pointer to local variable

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2006
Posts: 14
Reputation: ALAZHARY is an unknown quantity at this point 
Solved Threads: 1
ALAZHARY's Avatar
ALAZHARY ALAZHARY is offline Offline
Newbie Poster

Problem with Returning Pointer to local variable

 
0
  #1
Nov 25th, 2008
Hello, everybody.
first, look at the following code:

  1. #include <iostream>
  2.  
  3. 1 using std::cout;
  4. 2 using std::endl;
  5. 3 using std::cin;
  6. 4
  7. 5 double* treble(double);
  8. 6
  9. 7 int main(void)
  10. 8 {
  11. 9 double num = 5.0;
  12. 10 double* ptr = 0;
  13. 11 ptr = treble(num);
  14. 12 cout << endl
  15. 13 << "Three times num = " << 3.0*num << endl;
  16. 14 cout << "Result = " << *ptr;
  17. 15 cout << endl;
  18. 16 return 0;
  19. 17 }
  20. 18
  21. 19 double* treble(double data)
  22. 20 {
  23. 21 double result = 0.0;
  24. 22 result = 3.0*data;
  25. 23 return &result;
  26. 24 }

the output from this program is something like:

Three times num = 15
Result = 4.10416e-230

I know that the returned pointer of the function treble is point to local variable so when I returned, the value which inside that address has been lost.
there is no problem till here,
but let us to change the lines 13,14 to become:

  1. 13 << "Three times num = " << 3.0*num << endl
  2. 14 << "Result = " << *ptr;
the output will become:

Three times num = 15
Result = 15

why?
I'd give my right arm to Improve my english
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Problem with Returning Pointer to local variable

 
0
  #2
Nov 25th, 2008
The behaviour when dereferencing an invalid pointer is undefined. So I can only speculate on the underlying reason: it probably has to do with how and where stack variables are allocated, e.g. sequential function calls within a given scope might allocate stack variables from the same starting place, so stuff that's stack-allocated in the first call to cout << is likely to overwrite anything that was stack allocated on the prior call to treble. When it's done all in one call to cout <<, the value is fetched back before calling any other function, and thus before it's likely for it to have been overwritten, and it appears to work ok.

You can occasionaly predict how the thing is going to behave, e.g., I was pretty sure that this would happen:
  1. double num = 5.0;
  2. double* ptr = 0;
  3. ptr = treble(num);
  4. treble( 2.0 );
  5. cout << endl << "Three times num = " << 3.0*num << endl << "Result = " << *ptr;

Outputs:
Three times num = 15
Result = 6

On my machine, but I wouldn't bet on it being the same as yours, and I'd never rely on it working like that on mine.

A clearer example:
  1. #include <iostream>
  2.  
  3. int fn_a ( void )
  4. {
  5. int a = 0;
  6. std::cerr << &a << std::endl;
  7. }
  8.  
  9. int fn_b ( void )
  10. {
  11. int b = 0;
  12. std::cerr << &b << std::endl;
  13. }
  14.  
  15. int main ( void )
  16. {
  17. fn_a ( );
  18. fn_b ( );
  19.  
  20. return EXIT_SUCCESS;
  21. }

Outputs:
0xbff36764
0xbff36764

E.g. both function calls allocated a temporary variable in the same place, on my machine, today. (emphasis: "don't ever rely on this")

The important question, why are you doing this anyway?
  1. double treble(double data)
  2. {
  3. double result = 0.0;
  4. result = 3.0*data;
  5. return result;
  6. }
Is much more normal and safe.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: Problem with Returning Pointer to local variable

 
0
  #3
Nov 25th, 2008
result is a local variable, which is destroyed when the functions ends. So a garbage value is destroyed. The best solution is to do what is said by the poster above.

If you need to work with pointers you can do this:

  1. double* treble(double data)
  2. {
  3. double result = 0.0;
  4. result = 3.0*data;
  5. return new result;
  6. }

now result isn't destroyed and a pointer to it is returned. However, this memory must be freed manually.

delete ptr;
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem with Returning Pointer to local variable

 
0
  #4
Nov 25th, 2008
Originally Posted by minas1 View Post
result is a local variable, which is destroyed when the functions ends. So a garbage value is destroyed. The best solution is to do what is said by the poster above.

If you need to work with pointers you can do this:

  1. double* treble(double data)
  2. {
  3. double result = 0.0;
  4. result = 3.0*data;
  5. return new result;
  6. }

now result isn't destroyed and a pointer to it is returned. However, this memory must be freed manually.

delete ptr;
No errors means even number of errors
Incorrect example and wrong explanation (result always "destroyed": it's local automatic variable). Right "solution":
[code=c++]
double* treble(double data)
{
return new double(3.0*data);
}
Never, ever do that. It's totally senseless and a very dangerous programming style.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 14
Reputation: ALAZHARY is an unknown quantity at this point 
Solved Threads: 1
ALAZHARY's Avatar
ALAZHARY ALAZHARY is offline Offline
Newbie Poster

Re: Problem with Returning Pointer to local variable

 
0
  #5
Nov 26th, 2008
thank you minas1, but I think you need to read carefully what is the problem?
I'd give my right arm to Improve my english
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,832
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: Problem with Returning Pointer to local variable

 
0
  #6
Nov 26th, 2008
Originally Posted by ALAZHARY View Post
thank you minas1, but I think you need to read carefully what is the problem?
Actually: YOU need to read the replies carefully. MattEvans already told you what the problem is:

Originally Posted by MattEvans View Post
The behaviour when dereferencing an invalid pointer is undefined.
So just use the code posted by him and your problem will go away:
  1. double treble(double data)
  2. {
  3. double result = 0.0;
  4. result = 3.0*data;
  5. return result;
  6. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 14
Reputation: ALAZHARY is an unknown quantity at this point 
Solved Threads: 1
ALAZHARY's Avatar
ALAZHARY ALAZHARY is offline Offline
Newbie Poster

Re: Problem with Returning Pointer to local variable

 
0
  #7
Nov 26th, 2008
I understood MattEvans well, but he speculate, and minas1 didn't understand my question
I'd give my right arm to Improve my english
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Problem with Returning Pointer to local variable

 
1
  #8
Nov 26th, 2008
That "the behaviour when dereferencing an invalid pointer is undefined" is fact.

Other than that, the best you'll get (in terms of an explanation for why you get this output) is speculation or some implementation-specific reason. On my implementation (GCC) reasonable evidence (see second code I posted) suggests that local variables in any function get allocated from the same starting address (assuming the calls come from the same level).

(EDIT: and that would be sufficient explanation for your output, providing std::ostream::operator<< allocates at least one local variable)

There's no requirement for "reclaimed" memory to be zeroed or otherwise restored (by reclaimed, I mean memory under deleted pointers or under auto variables that go out of scope). The most efficient implementation strategy is to just leave the contents of such memory untouched, hence, before reclaimed memory is overwritten, it will tend to contain its old value. (You shouldn't rely on this, either)
Last edited by MattEvans; Nov 26th, 2008 at 7:46 am.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 14
Reputation: ALAZHARY is an unknown quantity at this point 
Solved Threads: 1
ALAZHARY's Avatar
ALAZHARY ALAZHARY is offline Offline
Newbie Poster

Re: Problem with Returning Pointer to local variable

 
0
  #9
Nov 26th, 2008
thank you very much MattEvans, your post was helpful.
I'd give my right arm to Improve my english
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC