直接使用运算符地址与使用指针变量返回局部变量的地址

Returning address of a local variable directly using address of operator vs using pointer variable

我知道您永远不应该 return 函数中局部变量的地址。但是在证明事实的同时我遇到了一个问题。考虑以下程序:

int *test()
{
    int x = 12;
    int *p = &x;
    return p;
}

int main()
{
    int *p = test();
    printf("%p",p);
    return 0;
}

它按预期打印了一个类似于 0061fed0 的地址。但是如果我 return x 的地址直接使用 & 即如果 test 函数更改如下:

int *test()
{
    int x = 12;
    return &x;
}

则输出变为00000000。那么,你能解释一下这里发生了什么吗?我正在使用与 Code:: Blocks 17.12 IDE in windows 10.

捆绑在一起的 gcc 编译器

关于重复问题: 建议的重复问题: 解释了直接使用运算符地址的行为,但没有解决指针变量用于 return 局部变量地址的场景,这在 StoryTeller 的回答中有解释:"The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime"。

严格来说,从C语言规范的角度来看,这是一个有效的结果。

6.2.4 Storage durations of objects (emphasis mine)

2 The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime. If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.

因此函数 returns 的值在任何一种情况下都是不确定的。您无法预测它会是什么,甚至无法以有意义的方式使用它。使用不确定值的程序具有未定义的行为,任何事情都可能发生。

因此,当您直接 return 本地地址时,您的编译器所做的是 return null。根据 C 语言本身,它是一个有效值,因为无论如何该对象很快就会死亡。但它的好处是可能会在现代托管实现的早期使您的程序崩溃,并允许您修复错误。