An experient derived from https://twitter.com/mikana/status/707246876546826240
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gcc --version | head -n1 | |
gcc (Gentoo 4.9.3 p1.2, pie-0.6.3) 4.9.3 | |
$ gcc -dumpmachine | |
x86_64-pc-linux-gnu | |
$ clang --version | head -n1 | |
clang version 3.7.1 (tags/RELEASE_371/final) | |
$ clang -dumpmachine | |
x86_64-pc-linux-gnu | |
$ cat > test.c | |
#include <stdio.h> | |
typedef struct { int i1; } s1; | |
typedef struct { int i2; } s2; | |
void f(s1 *s1p, s2 *s2p) | |
{ | |
s1p->i1 = 2; | |
s2p->i2 = 3; | |
printf("%i\n", s1p->i1); | |
} | |
int main(void) | |
{ | |
s1 s = { .i1 = 1 }; | |
f(&s, (s2 *)&s); | |
} | |
$ gcc -O0 -Wstrict-aliasing test.c ; ./a.out | |
3 | |
$ gcc -O0 -Wstrict-aliasing=2 test.c ; ./a.out | |
3 | |
$ gcc -O2 -Wstrict-aliasing test.c ; ./a.out | |
2 | |
$ gcc -O2 -Wstrict-aliasing=2 test.c ; ./a.out | |
test.c: In function ‘main’: | |
test.c:16:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] | |
f(&s, (s2 *)&s); | |
^ | |
2 | |
$ clang -O0 -Wstrict-aliasing test.c ; ./a.out | |
3 | |
$ clang -O0 -Wstrict-aliasing=2 test.c ; ./a.out | |
3 | |
$ clang -O2 -Wstrict-aliasing test.c ; ./a.out | |
3 | |
$ clang -O2 -Wstrict-aliasing=2 test.c ; ./a.out | |
3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment