#include <iostream>
int* retPointer()
{
int x=10; //same as auto int x =10;
return &x;
}
int main(int argc, char **argv)
{
std::cout<<"x="<<*retPointer()<<std::endl;
return 0;
}
In the example above I am returning a pointer that points to a local variable to stack. When I print the value pointed by this pointer in the man() then I should get a seg-fault. The variable x should go out of scope when I return from function retPointer(). So when try to access the value in main() should I not get a segmentation fault? Am I missing something here?
Output is = x=10