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?
delete
signature allow it to access the pointer, i.e. anything other than pass-by-value? – Mezuzahprintf()
it) afterdelete
is UB, so the user couldn't even legally read the pointer and compare to the original value. – Bellaudedelete
a pointer and it stores0xDEADBEEF
address after that. – Bellaudeoperator delete
) to delegate its work. And that the keyword has no 'signature'. – Parchdelete
. – Bellaudeoperator delete
and kin.) – Becketmemcmp()
it with the value afterdelete
will it not be undefined behavior? – Bellaudememcmp
doesn't use the pointer value, as far as I read the standard. It's not undefined behaviour tomemcmp
4 bytes that just so happen to invalid as a float value, so why should it be undefined tomemcmp
4 bytes that just so happen to be invalid as a pointer value? Memory can always be examined as bytes provided that it's still allocated, it doesn't matter what it does or doesn't represent. Just don't use the pointer value. The result of thememcmp
maybe unspecified or implementation-defined, though, whichever the standard says aboutdelete
on lvalues. – Fraasedelete
operator destructs the object before it callsoperator delete
to release the memory. – Turkestan