5

I don't understand how this example can possibly work:

double * GetSalary()  {
  double salary = 26.48; 
  return &salary;
}

main() {
    cout << *GetSalary();  //prints 26.48

}

salary is a local variable in GetSalary(), thus after returning from the function, this cell might possibly be overwritten by another function. I don't see how returning a pointer to a local variable (not instanciated on the heap) can ever possibly work.

8
  • 6
    Yes, it might possibly be overwritten. Or it might not. That's why this is called undefined behavior, rather than a guaranteed crash. Commented Jun 21, 2012 at 9:19
  • its funny that the obove code is the first result when I google "returning pointer form a function" Commented Jun 21, 2012 at 9:20
  • I often get the same thing. I have the idea that Google changes your search bubble when you're logged in to SO with a Google account. Commented Jun 21, 2012 at 9:21
  • @larsmans: I have got my question as first result (in few minutes), on a different browser (i.e not logged in google/stackoverflow)! Commented Jun 21, 2012 at 9:59
  • @VinayakGarg: ok, then I'm just being paranoid :) Commented Jun 21, 2012 at 10:06

6 Answers 6

16

It doesn't work. It is undefined behaviour. It may seem to work, because "correct behaviour" is a subset of "any possible behaviour".

Sign up to request clarification or add additional context in comments.

Comments

8

You're running into undefined behavior, which means anything can happen. Including appearing to work.

Outside the function, the return pointer is dangling (i.e. the memory it points to is invalid).

Why it appears to work boils down to the implementation. Most probably the memory isn't cleared. So although you don't have access to what the return pointer points to, in that memory the 26.48 still exists. But it's just by chance.

Comments

2
double * GetSalary()  
{   
     double salary = 26.48;    
     return &salary; 
}  
double dummy_function()
{
     double a = 1.1;
     double b = 2.2;
     double c = 0 , d = 0;

     c = a + b - d;
     return c;  
}

main() 
{     
     double *a;
     a = GetSalary();
     cout << dummy_function();
     cout << *a;  //this time it wont print 26.48
} 

Because function stack has been overwritten by the second function call dummy_function

Comments

1

It doesn't "work", it's dereferencing a pointer that is no longer valid. That the memory pointed at happens to hold the expected value is not a sign of the program as a whole "working" or being correct.

To understand why it happens to work, you need to analyze the exact pattern of stack frame shifts that take place, which is kind of annoying, and very compiler-dependent.

Comments

0

It is a dangling pointer which invokes undefined behaviour.
on some systems it might crash your application, on others it might appear to work correctly. But either way, you should not do it.
see this similar SO post.

Comments

0

This also works, but is much safer. It won't work right in a multi-threaded program.

double * GetSalary()  {
  static double salary = 26.48; 
  return &salary;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.