strict aliasing can not be turned off
Ian Lance Taylor
iant@google.com
Tue Aug 3 08:41:00 GMT 2010
"Liu, Lei" <lei.liu2@windriver.com> writes:
> Here is my program.
>
> #include <stdio.h>
>
> struct s1 {
> unsigned int a,b,c;
> };
> struct s2 {
> unsigned int b,c;
> };
>
> int main(void)
> {
> struct s1 s;
> s.c = 0xdead;
> struct s2 *p = (struct s2*)&s.b;
> p->c = 0xbeef;
> printf("s.c=%04x\n", s.c);
> return 0;
> }
>
> I compile this code with '-O2' flag and get a 's.c=dead'. It's
> reasonable under strict aliasing rule. But when I recompile it with
> -O2 -fno-strict-aliasing', the output is still 's.c=dead'.
I don't see this when I use -fno-strict-aliasing. I see the "s.c=beef"
as you expect. That is with gcc 4.4.3 on x86_64. What version of gcc
are you using?
Ian
More information about the Gcc-help
mailing list