Tomás wrote:
Kai-Uwe Bux posted:
[5.2.10/5] (expr.reinterpret.cast)
A value of integral type or enumeration type can be explicitly
converted to a pointer. A pointer converted to an integer of
sufficient size (if any such exists on the implementation) and back to
the same pointer type will have its original value; mappings between
pointers and integers are otherwise implementation-defined.
My query has nothing to do with integer-to-pointer conversion -- it's to
do with storing an invalid pointer value in a pointer variable.
That is a good question. Technically, the standard defined valid pointer
values as follows:
[3.9.2/3]
A pointer to objects of type T is referred to as a ?pointer to T.? [Example:
a pointer to an object of type int is referred to as ?pointer to int? and a
pointer to an object of class X is called a ?pointer to X.? ] Except for
pointers to static members, text referring to ?pointers? does not apply to
pointers to members. Pointers to incomplete types are allowed although
there are restrictions on what can be done with them (3.9). A valid value
of an object pointer type represents either the address of a byte in memory
(1.7) or a null pointer (4.10). If an object of type T is located at an
address A, a pointer of type cv T* whose value is the address A is said to
point to that object, regardless of how the value was obtained. [Note: for
instance, the address one past the end of an array (5.7) would be
considered to point to an unrelated object of the array?s element type that
might be located at that address. ] The value representation of pointer
types is implementation defined. Pointers to cv-qualified and
cv-unqualified versions (3.9.3) of layout-compatible types shall have the
same value representation and alignment requirements (3.9).
So, the standard acknowledges the possibility of not-pointing valid pointer
values (e.g., past end values of arrays).
However, it also knows a way of invalidating pointer values:
[3.7.3.2/4]
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.
(Funny: deallocating an array seems to not invalidate the past-end pointer.)
Now, you create a pointer value in a way that is not really foreseen by the
standard. Without calling the deallocation function, technically, the value
should be valid. But I would guess that it's not the intend of the
standard. I would guess, the indent is that the behavior is undefined.
Also, I do not see a way of deducing this from the wording.
Best
Kai-Uwe Bux