Inspired by this question.
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).
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?
No, it's not guaranteed and an implementation may legitimately assign zero to an lvalue operand to delete
.
Bjarne Stroustrup had hoped that implementations would choose to do this, but not many do.