c++ - 比较悬空指针是否合法?

标签 c++ pointers language-lawyer dangling-pointer

比较悬空指针是否合法?

int *p, *q;
{
    int a;
    p = &a;
}
{
    int b;
    q = &b;
}
std::cout << (p == q) << '\n';

注意 pq 如何指向已经消失的对象。这合法吗?

最佳答案

简介:第一个问题是使用p的值是否合法。

a 被销毁后,p 获取所谓的无效指针值。引自 N4430 (有关 N4430 状态的讨论,请参见下面的“注释”):

When the end of the duration of a region of storage is reached, the values of all pointers representing the address of any part of the deallocated storage become invalid pointer values.

使用无效指针值时的行为也包含在 N4430 的同一部分中(并且几乎相同的文本出现在 C++14 [basic.stc.dynamic.deallocation]/4 中):

Indirection through an invalid pointer value and passing an invalid pointer value to a deallocation function have undefined behavior. Any other use of an invalid pointer value has implementation-defined behavior.

[ Footnote: Some implementations might define that copying an invalid pointer value causes a system-generated runtime fault. — end footnote ]

因此,您需要查阅实现的文档以了解此处应该发生的情况(自 C++14 起)。

上述引用中的术语使用 means需要左值到右值的转换,如 C++14 [conv.lval/2]:

When an lvalue-to-rvalue conversion is applied to an expression e, and [...] the object to which the glvalue refers contains an invalid pointer value, the behaviour is implementation-defined.


历史:在 C++11 中,这表示 undefined 而不是 implementation-defined;它由 DR1438 更改.请参阅这篇文章的编辑历史以获取完整的引用。


应用到 p == q: 假设我们在 C++14+N4430 中接受了评估 pq 是实现定义的,并且实现没有定义发生硬件陷阱; [expr.eq]/2 说:

Two pointers compare equal if they are both null, both point to the same function, or both represent the same address (3.9.2), otherwise they compare unequal.

由于它是实现定义的,当评估 pq 时会获得什么值,所以我们不能确定这里会发生什么。但它必须是实现定义的或未指定的。

g++ 在这种情况下似乎表现出未指定的行为;取决于 -O 开关,我可以让它说 10,对应于相同的内存地址是否被重新- 在 a 被销毁后用于 b


关于 N4430 的注意事项:这是针对 C++14 提出的缺陷解决方案,尚未被接受。它清理了很多围绕对象生命周期、无效指针、子对象、 union 和数组边界访问的措辞。

在 C++14 文本中,在 [basic.stc.dynamic.deallocation]/4 和后续段落中定义 invalid pointer valuedelete 被使用。但是没有明确说明相同的原则是否适用于静态或自动存储。

在[basic.compound]/3中有一个“有效指针”的定义,但是用起来太模糊了。[basic.life]/5(脚注)指的是同一个文本来定义指针的行为适用于静态存储持续时间的对象,这表明它适用于所有类型的存储。

在 N4430 中,文本从该部分上移一级,以便明确适用于所有存储持续时间。附注:

Drafting note: this should apply to all storage durations that can end, not just to dynamic storage duration. On an implementation supporting threads or segmented stacks, thread and automatic storage may behave in the same way that dynamic storage does.


我的意见: 除了说 p 获得了无效的指针值之外,我没有看到任何一致的方式来解释标准(N4430 之前)。除了我们已经看过的内容之外,任何其他部分似乎都没有涵盖该行为。因此,在这种情况下,我很高兴将 N4430 措辞视为代表标准的意图。


关于c++ - 比较悬空指针是否合法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30694069/

相关文章:

c++ - 是否可以在我的对象上停止 std::addressof?

c++ - 是否有任何 C 或 C++ 标准识别内存映射文件的存在?

c - 程序可能检测 NULL 是用整数还是指针类型定义的方式?

c++ - enable_if 继承的成员函数的名称查找错误

c++ - 学习DirectX需要具备哪些知识

c++ - 如何从qt中的字符串中获取子字符串?

java - 难以理解具有嵌套函数的 Java 实现(并用 C++ 对其进行编码)

c - 具有两个空指针的结构的大小是 4?

c - 如何从参数返回内存指针以应用于夹板

c++ - void指针 'this'是什么意思?