gpt4 book ai didi

c++ - 局部变量的返回地址是错误的那么为什么这段代码有效

转载 作者:太空宇宙 更新时间:2023-11-04 16:26:09 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Can a local variable's memory be accessed outside its scope?

返回函数内部变量的地址是不好的,因为该变量将不再存在如果变量所属的栈帧结束。

那么为什么这段代码可以正常工作

int* test(){
int a = 11;
return &a;
}

int main(){

int *a;
a = test();

cout << *a;

return 0;

}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com