-fstrict-aliasing, -Wstrict-aliasing=2, and indirect conversions

Harald van Dijk truedfx@gentoo.org
Tue Jul 26 23:52:00 GMT 2005


Hello,

int main() {
     int a = 1;
     short *b = (short *) (char *) &a;
     *b = 2;
     return a;
}

This simple program returns 1 when compiled with -O2 -fstrict-aliasing, 
and 2 when compiled with -O2 -fno-strict-aliasing. It does not produce 
any warnings with -Wstrict-aliasing=2. According to the documentation:

`-Wstrict-aliasing=2'
      This option is only active when `-fstrict-aliasing' is active.  It
      warns about all code which might break the strict aliasing rules
      that the compiler is using for optimization.  This warning catches
      all cases, but it will also give a warning for some ambiguous
      cases that are safe.

Is this a bug? If so, what is the bug? Is the warning missing, or is the 
optimization invalid? And are there any alternative options that really 
catch every aliasing problem?

I'm using gcc 4.0.1 on i686-pc-linux-gnu.

Thanks in advance for any replies.



More information about the Gcc-help mailing list