在 c++0x 中删除 nullptr 仍然安全吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6731331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it still safe to delete nullptr in c++0x?
提问by Mankarse
In c++03it is pretty clear that deleting a null pointer has no effect. Indeed, it is explicitly stated in §5.3.5/2that:
在c++03它是很清楚,删除一个空指针没有任何效果。事实上,它明确指出§5.3.5/2:
In either alternative, if the value of the operand of delete is the null pointer the operation has no effect.
在任一替代方案中,如果 delete 的操作数的值是空指针,则操作无效。
However, in the current draftfor c++0xthis 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-expressionis not the null pointer constant. Is deleting the null pointer still defined in c++0x, and if so, where?
但是,在目前的草案中c++0x似乎缺少这句话。在草案的其余部分,我只能找到说明如果删除表达式的操作数不是空指针常量会发生什么的句子。是否删除仍定义在 中的空指针c++0x,如果是,在哪里?
Notes:
笔记:
There is significant circumstantial evidence to suggest that it is still well defined.
有重要的间接证据表明它的定义仍然很好。
First, there are the two sentences in §5.3.5/2stating that
首先,有两句话在§5.3.5/2说明
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 0is 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++0xdraft. Annex C is however an Informative section, so this has no bearing no the interpretation of the standard.
其次,改变 的含义delete 0是一个重大的突破性变化,标准委员会不太可能做出这种特殊的变化。此外,c++0x草案的兼容性附件(附件 C)中没有提到这是一个重大变化。然而,附录 C 是信息性部分,因此这与标准的解释无关。
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.
另一方面,删除空指针需要无效的事实意味着额外的运行时检查。在很多代码中,操作数永远不能为空,因此这种运行时检查与零开销原则相冲突。也许委员会只是决定改变行为,使标准 c++ 更符合语言的既定设计目标。
回答by interjay
5.3.5/7 says:
5.3.5/7 说:
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 unspeci?ed whether the deallocation function will be called.
如果删除表达式的操作数的值不是空指针值,则删除表达式将调用释放函数(3.7.4.2)。否则,未指定是否会调用释放函数。
And 3.7.4.2/3 says:
3.7.4.2/3 说:
The value of the ?rst 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.
所以行为是明确定义的,只要使用标准的释放函数,或者用户提供的释放函数正确处理空指针。
回答by David Hammen
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 mustmake 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.
同样值得注意的是:旧标准自相矛盾,它说(5.3.5/2)“如果删除的操作数的值是空指针,则操作无效”,但后来又说(5.3.5/7) “删除表达式将调用释放函数。” 调用函数是一种效果。尤其如此,因为被调用的函数很可能是一个被覆盖的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.
新的措辞消除了这种矛盾,明确地将它留给实现来决定是否在删除空指针的情况下调用释放函数。

