调用对NULL指针的删除 - C ++ 03对比C ++ 11 [英] Calling delete on NULL pointers - C++03 vs C++11

查看:168
本文介绍了调用对NULL指针的删除 - C ++ 03对比C ++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 03标准中,我看到:


5.3.5删除 >

2如果操作数具有类类型,则通过调用上述转换函数将操作数转换为指针类型,并使用转换后的操作数代替原始操作数对于本节的剩余部分。 在任一替代方法中,如果 delete 的操作数的值是空指针,则操作不起作用。删除对象), delete 的操作数的值应为指向非数组对象的指针或指向表示这种对象的基类(第10节)。如果不是,行为是未定义的。在第二种替代方案( delete array )中, delete 的操作数的值应是从前一个数组得到的指针值new-expression


在C ++ 11草案标准(N3337)中,我看到: / p>


5.3.5删除



操作数具有类类型,通过调用上述转换函数将操作数转换为指针类型,并且使用转换的操作数代替本节其余部分的原始操作数。在第一替代(删除对象)中, delete 的操作数的值可以是空指针值<指向由先前的 new-expression 创建的非数组对象的指针,或指向表示此类对象的基类(子句10)的子对象(1.8)的指针。如果不是,行为是未定义的。在第二替代方案( delete array )中,delete的操作数的值可以是空指针值或从先前数组 new-expression 产生的指针值。如果没有,行为是未定义的。


我突出显示了两个标准的规格之间的差异。我发现奇怪的是,2003年的标准更加强调如何处理NULL指针,而2011年的标准没有说明什么是实现必须做的。


  1. C ++ 11标准的标准在标准草案和实际标准之间是否有所不同?如果是,如何?


  2. 如果草案标准的语句在实际标准中保持不变,将非常强的语句更改为几乎没有什么是什么



解决方案

看起来我们可以找到理由对于缺陷报告348中的此更改 a>,表示:


具体来说,标准在5.3.5 [expr.delete]第2段中说:


...如果delete的操作数的值是空指针,则操作不起作用。


标准没有指定术语没有效果。从
这个上下文不清楚,被调用的释放函数是否需要到
没有效果,或者delete-expression不会调用释放
函数。



此外,在第4段标准中对默认解除分配函数说:


释放
函数(3.7.4.2 [basic.stc.dynamic.deallocation]),如果
的操作数delete表达式不是空指针常量,...


为什么在默认解除分配函数和delete-expr的交互上如此具体?



效果是解除分配函数的要求,那么
应该在3.7.4.2 [basic.stc.dynamic.deallocation]中声明,或者在
18.6.1.1 [new.delete.single]中声明, 18.6.1.2 [new.delete.array],并且应该明确声明。


部分解决方法是改变措辞你注意到,虽然该短语周围的语言已经改变了很多,但骑车的逻辑没有效果语言仍然存在,它不是一个很好定义的术语,所以应该替换为良好的指定语言。


In the C++03 Standard, I see:

5.3.5 Delete

2 If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. In the first alternative (delete object), the value of the operand of delete shall be a pointer to a non-array object or a pointer to a sub-object (1.8) representing a base class of such an object (clause 10). If not, the behavior is undefined. In the second alternative (delete array), the value of the operand of delete shall be the pointer value which resulted from a previous array new-expression.72) If not, the behavior is undefined.

In the C++11 Draft Standard (N3337), I see:

5.3.5 Delete

2 If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In the first alternative (delete object), the value of the operand of delete may be a null pointer value, a pointer to a non-array object created by a previous new-expression, or a pointer to a subobject (1.8) representing a base class of such an object (Clause 10). If not, the behavior is undefined. In the second alternative (delete array), the value of the operand of delete may be a null pointer value or a pointer value that resulted from a previous array new-expression. If not, the behavior is undefined.

I have highlighted the differences between the specifications in the two standards. I find it strange that the 2003 standard was more emphatic on how NULL pointers must be dealt with while the 2011 standard says nothing about what an implementation must do.

  1. Did the verbiage of the C++11 standard change between the draft standard and the actual standard? If so, how?

  2. If the verbiage of the draft standard remains unchanged in the actual standard, what was the rationale for changing a very strong statement to almost nothing between 2003 and 2011?

解决方案

It looks like we can find a rationale for this change in defect report 348, which says:

Specifically, standard says in 5.3.5 [expr.delete] paragraph 2:

...if the value of the operand of delete is the null pointer the operation has no effect.

Standard doesn't specify term "has no effect". It is not clear from this context, whether the called deallocation function is required to have no effect, or delete-expression shall not call the deallocation function.

Furthermore, in para 4 standard says on default deallocation function:

If the delete-expression calls the implementation deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]), if the operand of the delete expression is not the null pointer constant, ...

Why it is so specific on interaction of default deallocation function and delete-expr?

If "has no effect" is a requirement to the deallocation function, then it should be stated in 3.7.4.2 [basic.stc.dynamic.deallocation], or in 18.6.1.1 [new.delete.single] and 18.6.1.2 [new.delete.array], and it should be stated explicitly.

part of the resolution was the change in wording that you noted, although the language around that phrasing has changed quite a bit but the logic of getting ride of the has no effect language still stands, it is not a well defined term and so should be replaced with well specified language.

这篇关于调用对NULL指针的删除 - C ++ 03对比C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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