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

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

问题描述

c ++ 03 中,很清楚删除空指针没有效果。实际上,在§5.3.5/ 2 中明确表示:


在任一替代方案中,如果delete的操作数的值是空指针,则操作没有效果。


草稿 c ++ 0x 这句话似乎丢失了。在草稿的其余部分,我只能找到句子,说明如果 delete-expression 的操作数不是空指针常量,会发生什么。删除 c ++ 0x 中仍定义的空指针,如果是,则<?p> 注意:



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



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


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



在第二个选项删除的操作数可以是空指针值或...


这些表示操作数允许为null,



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



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

解决方案

5.3.5 / 7说:


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


而3.7.4.2/3说:


提供给释放函数的第一个参数的值可以是空指针值;如果是,并且如果解除分配函数是在标准库中提供的,则调用没有效果。


定义,只要使用标准释放功能,或用户提供的释放功能正确处理空指针。


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.

解决方案

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆