I rewrote your example to issue a warning about breaking anti-aliasing rules:
void foo(int* pi) { short* j = (short*)pi; j[0] = j[1] = 0; } int main() { int i = 1234; foo(&i); short* j = (short*)&i; j[0] = j[1] = 0; }
Although g ++ 4.6 only shows a warning, if you compile code with -Wstrict-aliasing=2 instead of -Wstrict-aliasing . In addition, it only shows a warning for translation in main() , and not in foo() . But I do not see how / why the compiler will look at these two roles differently.
source share