开发者

c++ warning: address of local variable

开发者 https://www.devze.com 2022-12-31 18:49 出处:网络
Merged with Can a local variable's memory be accessed outside its scope?. 开发者_开发技巧 int * ref () {
Merged with Can a local variable's memory be accessed outside its scope?. 开发者_开发技巧
int * ref () {

 int tmp = 100;
 return &tmp;
}

int main () {

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

I know the function ref () is allocated stack space. It will get destroyed as soon as the function exits. So the complier will give warning information. But my question is why the returning result is still correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号