Hi All,
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'. It seems that
'-fno-strict-aliasing' does not take effect on the code. The compiler
generated code are the same:
8048401: c7 44 24 08 ad de 00 movl $0xdead,0x8(%esp)
8048408: 00
8048409: c7 44 24 04 f0 84 04 movl $0x80484f0,0x4(%esp)
8048410: 08
8048411: c7 04 24 01 00 00 00 movl $0x1,(%esp)
8048418: e8 eb fe ff ff call 8048308 <__printf_chk@plt>
How can I disable strict aliasing when -O2 is enabled and get the
expected result 's.c=beef'.
Thanks.
Lei