Alan McKenney
2007-10-29 22:22:16 UTC
A while back I asked on comp.lang.c++.moderated the question:
if you have pointers to two different objects and you cast them
to void *, is it legal for them to compare equal?
Many people seemed to think it was not, but none of their
explanations seemed all that convincing to me.
The situation I envisioned where this could come up is
a word-addressed machine where one word contains
multiple characters (let's say 8.)
In that case, a sensible way of representing pointers to char
types would be to have the index within the word in the bottom three
bits and the pointer to the word in the higher-order bits.
Pointers to other built-in types and to objects would be word
pointers.
Next, it would make sense to have cast to "void *" just copy the
bits in the pointer -- why do extra work?
But if you do it this way, then you could have a pointer to char
and a pointer to a double be the same, if the double's word address
was 8 times the char's word address.
If the Standard required that the void * representation of pointers
to different objects always compared unequal, it would essentially
mean that the implementation would have to use the char *
representation for the void * representation of any pointer -- meaning
conversion between void * and normal pointer would involve shifts.
So, what does the Standard require here?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
if you have pointers to two different objects and you cast them
to void *, is it legal for them to compare equal?
Many people seemed to think it was not, but none of their
explanations seemed all that convincing to me.
The situation I envisioned where this could come up is
a word-addressed machine where one word contains
multiple characters (let's say 8.)
In that case, a sensible way of representing pointers to char
types would be to have the index within the word in the bottom three
bits and the pointer to the word in the higher-order bits.
Pointers to other built-in types and to objects would be word
pointers.
Next, it would make sense to have cast to "void *" just copy the
bits in the pointer -- why do extra work?
But if you do it this way, then you could have a pointer to char
and a pointer to a double be the same, if the double's word address
was 8 times the char's word address.
If the Standard required that the void * representation of pointers
to different objects always compared unequal, it would essentially
mean that the implementation would have to use the char *
representation for the void * representation of any pointer -- meaning
conversion between void * and normal pointer would involve shifts.
So, what does the Standard require here?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]