在 c++0x 中删除 nullptr 仍然安全吗?

发布于 2024-11-24 10:03:12 字数 930 浏览 5 评论 0原文

c++03中,很明显删除空指针没有任何效果。事实上,§5.3.5/2 中明确指出:

无论哪种选择,如果delete操作数的值为空指针,则该操作无效。

但是,在 c++0x 的当前草案这句话好像少了。在草稿的其余部分中,我只能找到一些句子来说明如果删除表达式的操作数不是空指针常量会发生什么情况。删除空指针是否仍然在 c++0x 中定义,如果是,在哪里?

注释:

有重要的间接证据表明它仍然有明确的定义。

首先,§5.3.5/2中有两句话指出

第一种替代方案(删除对象)中,delete操作数的值可能是空指针值,...

并且

在第二种选择(删除数组)中,delete操作数的值可能是空指针值或...

这些表示允许操作数为空,但它们本身实际上并没有定义如果为空会发生什么。

其次,改变delete 0的含义是一个重大的突破性改变,标准委员会不太可能做出这一特定的改变。此外,c++0x 草案的兼容性附件(附件 C)中没有提及这是一个重大更改。然而,附录 C 是信息性部分,因此不承担对标准的解释。

另一方面,删除空指针必须没有任何效果,这一事实意味着需要进行额外的运行时检查。在很多代码中,操作数永远不能为空,因此这种运行时检查与零开销原则相冲突。也许委员会只是决定改变行为,以使标准 c++ 更符合该语言既定的设计目标。

In c++03 it is pretty clear that deleting a null pointer has no effect. Indeed, it is explicitly stated in §5.3.5/2 that:

In either alternative, if the value of the operand of delete is the null pointer the operation has no effect.

However, in the current draft for c++0x this sentence seems to be missing. In the rest of the draft I could only find sentences stating what happens if the operand of the delete-expression is not the null pointer constant. Is deleting the null pointer still defined in c++0x, and if so, where?

Notes:

There is significant circumstantial evidence to suggest that it is still well defined.

First, there are the two sentences in §5.3.5/2 stating that

In the first alternative (delete object), the value of the operand of delete may be a null pointer value, ...

and

In the second alternative (delete array), the value of the operand of delete may be a null pointer value or ...

These say that the operand is allowed to be null, but on their own do not actually define what happens if it is.

Second, changing the meaning of delete 0 is a major breaking change, and the standards committee would be very unlikely make this particular change. Furthermore there is no mention of this being a breaking change in the Compatibility Annex (Annex C) of the c++0x draft. Annex C is however an Informative section, so this has no bearing no the interpretation of the standard.

On the other hand, the fact that deleting the null pointer is required to have no effect implies an additional run-time check. In a lot of code the operand can never be null, so this runtime check is in conflict with the zero overhead principle. Maybe the committee just decided to change the behaviour to bring standard c++ more in line with the stated design goals of the language.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小镇女孩 2024-12-01 10:03:45

另一方面,删除空指针必须无效这一事实意味着需要进行额外的运行时检查。

新措辞不会删除对空指针的运行时检查。反过来说:标准草案更接近于说实现必须进行空指针测试才能兼容。

另外值得注意的是:旧标准自相矛盾,它说(5.3.5/2)“如果delete操作数的值为空指针,则操作没有效果”,但后来又说(5.3.5/7) “删除表达式将调用释放函数。”调用函数就是一种效果。尤其如此,因为调用的函数很可能是重写的操作符删除

新的措辞消除了这一矛盾,明确地将在删除空指针的情况下是否调用释放函数留给实现。

On the other hand, the fact that deleting the null pointer is required to have no effect implies an additional run-time check.

The new wording does not remove that run-time check for a null pointer. The other way around: draft standard comes even closer to saying that an implementation must make a null pointer test to be compliant.

Also noteworthy: The old standard contradicted itself in that it said (5.3.5/2) that "if the value of the operand of delete is the null pointer the operation has no effect" but later said that (5.3.5/7) the "delete-expression will call a deallocation function." Calling a function is an effect. This is particularly so since the function that is called might well be an overridden operator delete.

The new wording removes that contradiction, explicitly leaving it up to the implementation whether the deallocation function is called in the case of deleting a null pointer.

落叶缤纷 2024-12-01 10:03:43

5.3.5/7 说:

如果删除表达式的操作数的值不是空指针值,则删除表达式将调用释放函数 (3.7.4.2)。否则,未指定是否会调用释放函数。

3.7.4.2/3 说:

提供给释放函数的第一个参数的值可以是空指针值;如果是这样,并且释放函数是标准库中提供的函数,则调用无效。

因此,只要使用标准释放函数,或者用户提供的释放函数正确处理空指针,行为就得到了很好的定义。

5.3.5/7 says:

If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will call a deallocation function (3.7.4.2). Otherwise, it is unspecified whether the deallocation function will be called.

And 3.7.4.2/3 says:

The value of the first argument supplied to a deallocation function may be a null pointer value; if so, and if the deallocation function is one supplied in the standard library, the call has no effect.

So the behavior is well defined, as long as the standard deallocation function is used, or a user-provided deallocation function handles null pointers correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文