Pointers in c++ after delete

Both:

A* c = a;
A* d = b;

are undefined in C++11 and implementation defined in C++14. This is because a and b are both “invalid pointer values” (as they point to deallocated storage space), and “using an invalid pointer value” is either undefined or implementation defined, depending on the C++ version. (“Using” includes “copying the value of”).

The relevant section ([basic.stc.dynamic.deallocation]/4) in C++11 reads (emphasis added):

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, rendering invalid all pointers referring to any part of the deallocated storage. The effect of using an invalid pointer value (including passing it to a deallocation function) is undefined.

with a non-normative note stating:

On some implementations, it causes a system-generated runtime

In C++14 the same section reads:

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, rendering invalid all pointers referring to any part of the deallocated storage. Indirection through an invalid pointer value and passing an invalid pointer value to a deallocation function have undefined behavior. Any other use of an invalid pointer value has implementation-defined behavior.

with a non-normative note stating:

Some implementations might define that copying an invalid pointer value causes a system-generated runtime fault

Leave a Comment