C++警告:局部变量

时间:2021-07-31 17:25:31

标签: c++

#include <iostream>
using namespace std;
int * ref () {

 int tmp = 100;
 return &tmp;
}

int main () {

 int * a = ref();
 cout << *a << endl;
}

我知道函数ref()是分配栈空间的。一旦函数退出,它就会被销毁。所以编译器会给出警告信息。但我的问题是为什么返回的结果仍然正确?

0 个答案:

没有答案
相关问题