Possible Duplicate:
Can a local variable's memory be accessed outside its scope?
#include <iostream>
double *foo(){
double *varFoo = new double;
double temp = 8762;
varFoo = &temp;
return varFoo;
}
int main(void){
double *newVar = foo();
std::cout<<*newVar<<std::endl;
std::cin.get();
return 0;
}
I understand that the pointer varFoo will be created in the heap and thus will stay there until I call delete, but what about temp variable which is inside the function foo?
it's a local variable and as soon as the call of the foo function ends, the address where the temp variable's values will be stored will just be freed right?
so why do I get 8762 as a result instead of rubbish?
thanks