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

Is it still safe to delete nullptr in c++0x?(在 c++0x 中删除 nullptr 仍然安全吗?)

本文介绍了在 c++0x 中删除 nullptr 仍然安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

无论哪种方式,如果 delete 的操作数的值是空指针,则操作无效.

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

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

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?

注意事项:

有重要的间接证据表明它的定义仍然很好.

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

首先,§5.3.5/2中有两句话说明

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

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

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

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.

其次,改变delete 0 的含义是一个重大的突破性变化,标准委员会不太可能做出这种特殊的变化.此外,在 c++0x 草案的兼容性附件(附录 C)中没有提到这是一个重大变化.然而,附录 C 是信息性部分,因此与标准的解释无关.

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.

另一方面,删除空指针需要无效的事实意味着额外的运行时检查.在很多代码中,操作数永远不能为空,因此这种运行时检查与零开销原则相冲突.也许委员会只是决定改变行为,使标准 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.

推荐答案

5.3.5/7 说:

如果delete-expression 的操作数的值不是空指针值,则delete-expression 将调用释放函数(3.7.4.2).否则,未指定是否调用释放函数.

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.

3.7.4.2/3 说:

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.

这篇关于在 c++0x 中删除 nullptr 仍然安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 c++0x 中删除 nullptr 仍然安全吗?

基础教程推荐