灵感来自这个问题.
假设在 C++ 代码中我有一个有效的指针并正确地删除它.根据 C++ 标准,指针将变为无效(3.7.3.2/4 - 释放函数将使指向释放存储的所有部分的所有指针无效).
Suppose in C++ code I have a valid pointer and properly delete it. According to C++ standard, the pointer will become invalid (3.7.3.2/4 - the deallocation function will render invalid all pointers referring to all parts of deallocated storage).
至少在大多数实现中,它会保留该值,并将存储与 delete 之前完全相同的地址,但是 使用该值是未定义的行为.
At least in most implementations it preserves the value and will store exactly the same address as before delete, however using the value is undefined behavior.
标准是否保证指针将保留其值或允许值更改?
Does the standard guarantee that the pointer will preserve its value or is the value allowed to change?
不,不能保证,实现可以合法地将零分配给 lvalue 操作数以delete.
No, it's not guaranteed and an implementation may legitimately assign zero to an lvalue operand to delete.
Bjarne Stroustrup 曾希望实现会选择这样做,但不是很多.
Bjarne Stroustrup had hoped that implementations would choose to do this, but not many do.
http://www.stroustrup.com/bs_faq2.html#delete-zero
这篇关于指针是否保证在 C++ 中的“删除"之后保留其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
std::reference_wrapper 和简单指针的区别?Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和简单指针的区别?)
常量之间的区别.指针和引用?Difference between const. pointer and reference?(常量之间的区别.指针和引用?)
c++ - 如何从指向向量的指针访问向量的内容?How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何从指向向量的指针访问向量的内容?)
*& 的含义和**&在 C++ 中Meaning of *amp; and **amp; in C++(*amp; 的含义和**amp;在 C++ 中)
为什么我不能对普通变量进行多态?Why can#39;t I do polymorphism with normal variables?(为什么我不能对普通变量进行多态?)
取消引用已删除的指针总是会导致访问冲突?Dereferencing deleted pointers always result in an Access Violation?(取消引用已删除的指针总是会导致访问冲突?)