简体   繁体   English

在C ++标准中它说的是什么:: delete可以改变左值?

[英]Where in the C++ Standard does it say ::delete can change lvalues?

I ran into my first compiler that changes the lvalue passed to ::delete, but doesn't zero out the lvalue. 我遇到了第一个改变传递给:: delete的左值的编译器,但没有将左值归零。 That is the following is true: 这是以下情况:

 Foo * p = new Foo();
 Foo * q = p;
 assert(p != 0);
 assert(p == q);
 ::delete p;
 assert(p != q);
 assert(p != 0);

Note that p is not zero after the delete operation, and it has changed from it's old value. 请注意,删除操作后p不为零,并且已从其旧值更改。 A coworker told me that this is not unusual in his experience having worked with some mainframe C++ compilers that would change p to 0xFFFFFFFF, as well as other compilers that would change p to 0. 一位同事告诉我,他在使用一些将p改为0xFFFFFFFF的大型机C ++编译器以及将p改为0的其他编译器的经验并不罕见。

Where in the C++ Standard does it say that a compiler is allowed to do this? 在C ++标准中,它是否允许编译器执行此操作?

Searching through StackOverflow, I found this question: Why doesn't delete set the pointer to NULL? 通过StackOverflow搜索,我发现了这个问题: 为什么不删除将指针设置为NULL? which had an answer that referred to Bjarne Stroustrup's response that includes the statement: 有一个答案提到Bjarne Stroustrup的答复 ,其中包括声明:

C++ explicitly allows an implementation of delete to zero out an lvalue operand, and I had hoped that implementations would do that, but that idea doesn't seem to have become popular with implementers. C ++显然允许delete的实现将左值操作数归零,我曾希望实现会这样做,但这个想法似乎并没有受到实现者的欢迎。

I've read and re-read section 5.3.5 and 12.5 of the final committee draft C++0x standard , but I'm not seeing the "explicit" part. 我已经阅读并重新阅读了最终委员会草案C ++ 0x标准的第5.3.5和12.5节,但我没有看到“明确”部分。 Am I just looking in the wrong sections of the standard? 我只是在寻找标准的错误部分吗? Or is there a chain of logic that is in the sections but I'm just not connecting together properly. 或者这些部分中是否存在一系列逻辑,但我只是没有正确连接在一起。

I don't have my copy of the Annotated C++ Reference Manual anymore. 我没有Annotated C ++ Reference Manual的副本了。 Was it in the ARM that a compiler could do this? 编译器是否可以在ARM中执行此操作?

[Edit: Correcting the section reference from 3.5.3 to 5.3.5. [编辑:将部分参考从3.5.3更正为5.3.5。 I'm also adding an interesting paradox as a counterpoint to Henk's assertion that p is undefined after delete.] 我还添加了一个有趣的悖论作为Henk断言删除后p未定义的对立点。

There is an interesting paradox if p is initialized to null. 如果将p初始化为null,则存在一个有趣的悖论。

 Foo * p = 0;
 Foo * q = p;
 assert(p == 0);
 assert(p == q);
 ::delete p;
 assert(p == q);
 assert(p == 0);

In this case though, the behavior is well documented. 但在这种情况下,行为已有详细记录。 When delete gets a null pointer, it is suppose to do nothing, so p remains unchanged. 当delete获得一个空指针时,它假设什么也不做,所以p保持不变。

It might not be so explicit. 它可能不那么明确。 In 5.3.5/7 it says that the delete expression will call a deallocator function. 在5.3.5 / 7中,它表示delete表达式将调用deallocator函数。 Then in 3.7.3.2/4 it says that using a pointer that has been deallocated is undefined. 然后在3.7.3.2/4中,它说使用已经解除分配的指针是未定义的。 Since the value of the pointer cannot be used after the deallocation, then whether the pointer keeps the value or the value is changed by the implementation does not make a difference. 由于在重新分配后不能使用指针的值,因此指针是保持值还是由实现改变了值没有区别。

5.3.5/7 5.3.5 / 7

The delete-expression will call a deallocation function (3.7.3.2). delete-expression将调用释放函数 (3.7.3.2)。

3.7.3.2/4 3.7.3.2/4

If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value (4.10), the deallocation function shall deallocate the storage referenced by the pointer, render- ing invalid all pointers referring to any part of the deallocated storage. 如果给标准库中的释放函数赋予的参数是一个不是空指针值的指针(4.10),则释放函数将释放指针引用的存储,使所有指向引用任何部分的指针无效。解除分配存储。 The effect of using an invalid pointer value (including passing it to a deallocation function) is undefined . 使用无效指针值(包括将其传递给释放函数)的效果未定义

The references are from the current standard. 参考文献来自现行标准。 In the upcoming standard 5.3.5/7 has been reworded: 在即将推出的标准5.3.5 / 7中已经改写:

C++0x FD 5.3.5/7 C ++ 0x FD 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). 如果delete-expression的操作数的值不是空指针值,则delete-expression将调用释放函数(3.7.4.2)。 Otherwise, it is unspecified whether the deallocation function will be called. 否则,未指定是否将调用释放功能。 [ Note: The deallocation function is called regardless of whether the destructor for the object or some element of the array throws an exception. [注意:无论对象的析构函数或数组的某个元素是否引发异常,都会调用释放函数。 — end note ] - 结束说明]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++标准在哪里说,C在哪里说相同:编译单元(.cpp文件)中的变量按声明顺序初始化 - Where does the C++ standard say, and does C say the same: variables in a compilation unit (.cpp file) are initialised in order of declaration 在C ++标准中它表示必须初始化const内置类型变量的定义? - Where in the C++ Standard does it say that the definition of a const built-in type variable must be initialized? 在C ++标准中,它表示sizeof(wchar_t)<= sizeof(long)和sizeof(bool)<= sizeof(long)? - Where in the C++ Standard does it say that sizeof(wchar_t) <= sizeof(long) and sizeof(bool) <= sizeof(long)? C++ 标准究竟在哪里说取消引用未初始化的指针是未定义的行为? - Where exactly does C++ standard say dereferencing an uninitialized pointer is undefined behavior? 数字和字段宽度的格式化输出,C ++标准对此有何说明? - formatted output of a number and field width, where does the C++ standard say about it? C++ 标准对堆栈溢出有何规定? - What does the C++ standard say about stack overflow? C ++左值和右值 - C++ lvalues and rvalues 标准中哪里说下面的 typedef 是有效的? - Where in the standard does it say that the typedef below is valid? 在标准(C ++ 14)中它表示以下两个声明是等效的吗? - Where in the Standard (C++14) does it say that the following two declarations are equivalent? 在C ++ 14 Standard中,它表示非constexpr函数不能用于constexpr函数的定义吗? - Where in C++14 Standard does it say that a non-constexpr function cannot be used in a definition of a constexpr function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM