Reputation: 50061
In the comments to this answer I stumbled about a discussion about the meaning of "valid pointer". Since I think that that is interesting in general:
What is a "valid pointer" in C++?
In particular:
Is reinterpret_cast<const void*>(0x1)
a valid pointer?
Upvotes: 7
Views: 853
Reputation: 283733
The Standard places implementations into two general categories:
Your expression definitely is not a safely-derived pointer, so it's invalid in the first.
Quote from 3.7.4.3:
An implementation may have relaxed pointer safety, in which case the validity of a pointer value does not depend on whether it is a safely-derived pointer value. Alternatively, an implementation may have strict pointer safety, in which case a pointer value referring to an object with dynamic storage duration that is not a safely-derived pointer value is an invalid pointer value unless the referenced complete object has previously been declared reachable (20.7.4). [ Note: the effect of using an invalid pointer value (including passing it to a deallocation function) is undefined, see 3.7.4.2. This is true even if the unsafely-derived pointer value might compare equal to some safely-derived pointer value. — end note ] It is implementation defined whether an implementation has relaxed or strict pointer safety.
For implementations with relaxed safety, it doesn't matter how the pointer value is gotten, just that (3.9.2):
A valid value of an object pointer type represents either the address of a byte in memory (1.7) or a null pointer.
Is 0x1
a valid memory address on your system? Well, for some embedded systems it is. For most OSes using virtual memory, the page beginning at zero is reserved as invalid.
Upvotes: 10