C++ aliasing problem (noticed with -O2 -march=athlon)

Alexandre Oliva aoliva@redhat.com
Mon Dec 9 22:17:00 GMT 2002


On Dec  9, 2002, Jakub Bogusz <qboosh@pld.org.pl> wrote:

> "void*" should be valid alias for "unsigned int*", so
> isn't "void*&" vs "unsigned int*&" case similar?

Even though a void* can point to an unsigned int, you can't reference
it (you can't ever dereference a void*).  Arranging for a void** to
point to an unsigned* is asking for trouble, since dereferencing the
void** does violate aliasing rules, since void* and unsigned* are
different, unrelated types.  You could have a void* point to an
unsigned*, since the address of the unsigned* can be cast to void*,
just like any address, but you'd have to cast the pointer back to
unsigned** before you could dereference it.

For short: you're violating aliasing rules, and GCC is not in error
when it reorders the operations as it does on athlon.  It doesn't fail
on i686 out of luck.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                 aoliva@{redhat.com, gcc.gnu.org}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist                Professional serial bug killer



More information about the Gcc mailing list