差異

使っているのは Mac に付いてる gcc 4.0。man には -O2-fstrict-aliasing を含むと書いてあるんだけど、Apple の実装はやっぱり微妙に違っているらしい。

$ cat test.c
#include <stdio.h>
int main() {
    int zero = 42;
    short *p = (short *)&zero;
    p[0] = p[1] = 0;
    printf("zero = %d\n", zero);
}
$ gcc test.c && ./a.out 
zero = 0
$ gcc -O2 test.c && ./a.out 
zero = 0
$ gcc -O2 -fstrict-aliasing test.c && ./a.out 
zero = 42