K
Keith Thompson
[snip]Yes, for any given object type, two null pointers are guaranteed
to compare equal, even if they do not have the same internal
representation.
This is not -necessarily- as bad as the code that compares pointers
needing to iterate through all the different null pointer representations.
The compiler knows that pointer types at the time of the
comparison, so if some of the null pointer representations are not
possible for a given type, then the compiler would not need to
test that possibility. In particular, if there happens to be
exactly one null pointer representation per type (but that different
types might have different null pointer representations) then only
one test would need to be made.
This comparison process is potentially further simplified by the
fact that one cannot convert pointers directly between
incompatible types without going through void* [if I recall correctly].
The conversion into void* could produce a "canonical" null pointer
and the conversion of the "canonical" null pointer into a different
pointer type would choose one of the null pointer representations
valid for that type; in this way, one only has to deal with the
null pointer representations valid within the type. One thing to
watch out for here is that when a pointer is converted to void* and
converted back to the same type, the original pointer and the
double-converted one must compare equal -- but in the end this just
comes down to potentially needing to be able to compare multiple null
pointer representations for a single type.
No, you recall incorrectly. C99 6.3.2.3p7:
A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type.
(followed by caveats about undefined behavior for incorrect
alignment).
But a compiler could certainly implement pointer-to-pointer conversion
by converting to void* and then to the other type.