Skip to content

Instantly share code, notes, and snippets.

@omasanori
Last active March 10, 2016 02:56
Show Gist options
  • Save omasanori/2519364d3487b3c8f1a0 to your computer and use it in GitHub Desktop.
Save omasanori/2519364d3487b3c8f1a0 to your computer and use it in GitHub Desktop.
$ 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