A couple of days ago Jeff Stedfast ran into some problems with gcc 4.4, strict aliasing and optimizations. Being a geeky sort of person, I found the problem really interesting, not only because it shows just how hard it is to write a good, clear standard, even when you're dealing with highly technical (and supposedly unambiguous) language, but also because I never did "get" the aliasing rules, so it was a nice excuse to read up on the subject. Basically, the standard says that you can't do this:int a = 0x12345678;
short *b = (short *)&a;I'm forcing a cast here, and since the types are not compatible, they can't be "alias" of each other, and therefore I'm breaking strict-aliasing rules. Note that if you compile this with -O2 -Wall, it will *not* warn you that you're breaking the rules, even though -O2 activates -fstrict-aliasing and -Wall is supposed to complain about everything (right??). Apparently, this is by design, though why would anyone not want warnings in -Wall for something that will obviously break code is beyond me. If you want to be told that you're not playing by the rules, make sure you build with -Wstrict-aliasing=2, which will say: line 2 - warning: dereferencing type-punned pointer will break strict-aliasing rulesSo now you know you're being naughty. Of course, if you did try to access the variable, even just with -Wall it will complain at you - this more complete snippet will give you several warnings with -Wall: int a = 0x12345678;
short *b = (short *)&a;
b[1] = 0;
if (a == 0x12345678)
printf ("error\n");
else
printf ("good\n");line 3 - warning: dereferencing pointer ‘({anonymous})’ does break strict-aliasing rules
Read more: World of coding
short *b = (short *)&a;I'm forcing a cast here, and since the types are not compatible, they can't be "alias" of each other, and therefore I'm breaking strict-aliasing rules. Note that if you compile this with -O2 -Wall, it will *not* warn you that you're breaking the rules, even though -O2 activates -fstrict-aliasing and -Wall is supposed to complain about everything (right??). Apparently, this is by design, though why would anyone not want warnings in -Wall for something that will obviously break code is beyond me. If you want to be told that you're not playing by the rules, make sure you build with -Wstrict-aliasing=2, which will say: line 2 - warning: dereferencing type-punned pointer will break strict-aliasing rulesSo now you know you're being naughty. Of course, if you did try to access the variable, even just with -Wall it will complain at you - this more complete snippet will give you several warnings with -Wall: int a = 0x12345678;
short *b = (short *)&a;
b[1] = 0;
if (a == 0x12345678)
printf ("error\n");
else
printf ("good\n");line 3 - warning: dereferencing pointer ‘({anonymous})’ does break strict-aliasing rules
Read more: World of coding