Possible Duplicate:
Can a local variable's memory be accessed outside its scope?
I thought the following call to f() will get a pointer to a local memory that will not be handled by the compiler (which is dangerous according to the textbook). However, it still works well. Not sure whether this is safe or not.
#include <iostream>
using namespace std;
int * f()
{
int v[1000000];
for (int i=0; i<1000000; i++) v[i]=i;
cout<<v[7]<<endl;
return v;
}
int main()
{
int * v = f();
cout<<v[7]<<endl;
return 0;
}
Interesting question. Your code is likely to work (or seem to work) on some platforms and to fail on others.
The reason your code seems to work is that the memory the function
f()reserves on the stack is released but not erased whenf()returns. Being released, the memory becomes available for other functions to use; but it might not be overwritten until another function actually does use it.Some others here are correctly pointing out that your code evokes undefined behavior, and technically that is true. However, there is a reason you are getting the particular undefined behavior you are getting, and that is what my answer is about.
On some platforms, including the x86, after
f()releases the memory ofv[], the first memory to be reused will normally be the memory that used to holdv[999999]. It may be a long time before the memory that holdsv[0]gets reused. Hence, the data fromv[7]is spuriously still present.There is at least one more wrinkle. Some implementations, using some settings, may overwrite all released memory immediately with random data, to guard against security risks. (What if
v[]held a password, for example? The random data would wipe it safely away.)