The address of i
is never going to change in main()
, but the value contained therein will. You are taking the reference of a local variable and using it after that reference has fallen out of scope. (Imprecise language warning) The value 6
is on the stack. Since you didn't do anything with the stack after you put 6
there, the reference to it will still contain the same value. So, as others have said, you got lucky.
To see just how lucky, try running this code which uses the stack after you call foo()
:
#include <iostream>
#include <ctime>
#include <numeric>
int& foo()
{
int i = 6;
std::cout << &i << " = " << i << std::endl; //Prints the address of i before return
return i;
}
long post_foo(int f)
{
srand((unsigned)time(0));
long vals[10] = {0};
size_t num_vals = sizeof(vals)/sizeof(vals[0]);
for( size_t i = 0; i < num_vals; ++i )
{
int r = (rand()%2)+1;
vals[i] = (i+f)*r;
}
long accum = std::accumulate(vals, &vals[num_vals], 0);
return accum * 2;
}
int main()
{
int &i = foo();
// std::cout << "post_foo() = " << post_foo(i) << std::endl;
std::cout << &i << " = " << i << std::endl;
}
When I ran this with the post_foo()
call commented out, 6
was still on the stack and the output was:
002CF6C8 = 6
002CF6C8 = 6
...but when I un-commented the call to post_foo()
and ran it again, 6
was long gone:
001FFD38 = 6
post_foo() = 310
001FFD38 = 258923464