![](/img/trans.png)
[英]How to print out actual object value instead of memory address in gdb?
[英]The address always returned is 0 instead of the actual value
这是简单的代码:
int *ad_return()
{
int a=600;
cout << &a << endl;
return &a;
}
int main()
{
cout << ad_return();
return 0;
}
output 出乎意料。 第一个 cout 打印一个看起来像地址的地址,但主要的 cout 打印一个合法的 0。我在任何地方都找不到解释。
根据我的实验,我得出以下结论:
Since the address of a local variable that has already went out of scope is undefined, it seems that gcc and g++ decided to implement a feature that sets this to zero (probably to make sure that the code segfaults instead of generating thoroughly bizarre behaviour). 它在 clang 中没有这样做,但该值未定义,因此这两个编译器都按照标准运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.