在 c++0x 中删除 nullptr 仍然安全吗?
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
新措辞不会删除对空指针的运行时检查。反过来说:标准草案更接近于说实现必须进行空指针测试才能兼容。
另外值得注意的是:旧标准自相矛盾,它说(5.3.5/2)“如果delete操作数的值为空指针,则操作没有效果”,但后来又说(5.3.5/7) “删除表达式将调用释放函数。”调用函数就是一种效果。尤其如此,因为调用的函数很可能是重写的
操作符删除
。新的措辞消除了这一矛盾,明确地将在删除空指针的情况下是否调用释放函数留给实现。
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.
5.3.5/7 说:
3.7.4.2/3 说:
因此,只要使用标准释放函数,或者用户提供的释放函数正确处理空指针,行为就得到了很好的定义。
5.3.5/7 says:
And 3.7.4.2/3 says:
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.