其他分享
首页 > 其他分享> > c – 取消引用悬空指针是不确定的行为?

c – 取消引用悬空指针是不确定的行为?

作者:互联网

我无法在标准中找到该程序未定义的位置:

#include <iostream>

int main() 
{
    int *p;
    {
        int n = 45;
        p = &n;
    }
    std::cout << *p;
}

§3.8对象生命周期中的所有情况似乎都不适用于此处.

解决方法:

我不是100%肯定因为措辞,但看起来这是由3.8 / 6覆盖(我认为这种解释是正确的原因是因为3.8 / 5中的非规范性示例,//未定义的行为,生命周期of * pb已结束):

…after the lifetime of an object has ended and before the storage which the object occupied is reused or released, any glvalue that refers to the original object may be used but only in limited ways….The program has undefined behavior if:

然后第一个子弹是罪魁祸首:左值到右值的转换(4.1)应用于这样的glvalue,:这种转换必须在对操作符的调用点<<或者最后在ostream代码中读取整数值以进行格式化的点.

标签:c,language-lawyer,undefined-behavior
来源: https://codeday.me/bug/20190927/1824145.html