0

I'd like to know why is it that the data that is pointed to when you return from a function, is not deleted immediately. In the snippet of code below,I expect that when I return the address of x to the pointer to initialise it when returning from function, the data stored in the address of x should be deleted immediately. However I am still able to get the correct value in main and then only after one more line of code does it suddenly turn into garbage? Why does this not happen immediately

int* function(int x)
{
    cout << x << endl; // This outputs 5 as expected
    return &x;
}

int main()
{
    int* a = function(5);


    cout << *a; // This still outputs 5. Why?
    cout << endl;

    cout << *a; // It is garbage here as expected
    cout << endl;

    return 0;
}

Thanks

SBI
  • 2,322
  • 1
  • 17
  • 17
PiPatrol
  • 162
  • 2
  • 10
  • 1
    Variables are not deleted. Their values might become deleted. – Basile Starynkevitch Nov 12 '14 at 12:17
  • @Niall Even if it did: it wouldn't apply to local variables (at least in C++), and of course, garbage collection won't recycle the memory until it's needed. – James Kanze Nov 12 '14 at 12:17
  • 1
    As soon as x goes out of scope, accessing its contents is undefined as the location that stores them may be re-used. That doesn't mean they _are_ reused immediately -it can happen immediately, after a while or never – The Archetypal Paul Nov 12 '14 at 12:18
  • My [answer](http://stackoverflow.com/a/26705204/509868) to another similar question may help you. – anatolyg Nov 12 '14 at 12:19

4 Answers4

2

Returning the address of a local variable invokes undefined behavior. So, you're just lucky.

The local variable (allocated on the stack) gets deleted when it leaves it's scope, which in your case is when function() ends. However, after the variable leaves the scope, the memory it occupies is not rewritten immediately and unless that memory is reused the value of x will stay there.

Since it invokes UB, your attempt to read the contents of that memory can result in reading the right value, reading garbage, your programm crashing, or anything else.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
1

Returning address of local variable is undefined. Undefined means you can't predict whether it would output correct OR incorrect result. It's unfortunate that you got correct result.

ravi
  • 10,994
  • 1
  • 18
  • 36
0

As you should not be accessing such "garbage" (a technical term), the system is free to deal with it as it sees fit. For example, there may have been other tasks whose completion would have affected correctly-written programs.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

When you return the address of a local variable, the resulting pointer "dangles"; accessing through a dangling pointer, or even just reading it, is undefined behavior. Which means that in theory, anything can happen. In practice, the compiled code won't return the memory of the local variables in the function to the OS; in most OS's, it couldn't even if it wanted to. So the memory just sits there, until someone else uses it. In the case of memory in a function, it will be reused when you call another function; until then, you will probably be able to access it and find the old values. Debugging builds might intentionally scribble over the memory on leaving the function, but the mainstream compilers don't do this. And of course, programs like valgrind might check that the pointer is valid, and log an error (but I don't know if valgrind actually catches this).

James Kanze
  • 150,581
  • 18
  • 184
  • 329