加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

调用NULL指针上的删除 – C 03对C 11

发布时间:2020-12-16 03:23:16 所属栏目:百科 来源:网络整理
导读:在C 03标准中,我看到: 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 r
在C 03标准中,我看到:

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.

在C11标准草案(N3337)中,我看到:

5.3.5 Delete

2 If the operand has a class type,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 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.

我强调了两个标准中的规格之间的差异.我觉得奇怪的是,2003年的标准更重要的是如何处理NULL指针,而2011年的标准没有说明实现必须做什么.

>标准草案和实际标准之间的C11标准是否有变动?如果是这样,怎么样?
>如果标准草案的标准在实际标准中保持不变,那么在2003年至2011年间几乎没有任何变化的理由是什么呢?

解决方法

看来我们可以在 defect report 348这个变化中找到一个理由,它说:

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.


标准没有指定术语“没有效果”.不清楚
这个上下文,是否需要所谓的解除分配功能
没有效果,或者delete-expression不能调用解除分配
功能.

此外,在第4段标准中说明了默认的解除分配功能:

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,…


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

如果“没有效果”是解除分配功能的要求,那么
应该在3.7.4.2 [basic.stc.dynamic.deallocation]或者
18.6.1.1 [new.delete.single]和18.6.1.2 [new.delete.array],应该明确说明.

解决方案的一部分是您注意到的措辞的变化,虽然该短语的语言已经发生了很大的变化,但是驾驶的逻辑没有效果语言仍然存在,但这不是一个明确的术语,因此应该是用指定的语言替代.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读