int* pointBoom()//局部变量指针测试
{
int c = 20;
int *f = &c;//局部变量指针f
cout << "局部变量c的地址" << &c<<endl;
cout << "局部变量f存储的地址" << f<<endl;
cout << "局部变量f的地址" << &f<<endl;
return f;
}
int main()
{
cout << "调用函数pointBoom" << endl;
cout << "pointBoom的值是" << *pointBoom() << endl;
cout << "pointBoom中返回的地址是" << pointBoom() << endl;//第二次调用
return 0;
}
为什么程序正常,f指向的c是局部变量,c是在栈上分配的,函数调用结束后,不是销毁了么,f指向的内存地址中存储的变量不是为空么。为什么还能正常输出呢?