In message <[EMAIL PROTECTED]>, TOMITA Yoshinori writes: >For the source code below, compiling gcc -O2/-O3 seem to produce >incorrect code. > >----------------------------------- >#include <stdio.h> >int main(int argc, char* argv[]) >{ > unsigned int x = 0x12345678; > unsigned short tmp; > printf("%x\n", x); > tmp = ((unsigned short *)&x)[0]; > ((unsigned short *)&x)[0] = ((unsigned short *)&x)[1]; > ((unsigned short *)&x)[1] = tmp; > printf("%x\n", x); > return 0; >} >-----------------------------------
>% gcc -O2 a.c >% ./a.out >12345678 >12341234 % sed 's/unsigned int/unsigned volatile int/' a.c > b.c % gcc -O2 b.c % ./a.out 12345678 56781234 Your code forgot to tell the compiler that you would be messing with the variables storage directly. -- Poul-Henning Kamp | UNIX since Zilog Zeus 3.20 [EMAIL PROTECTED] | TCP/IP since RFC 956 FreeBSD committer | BSD since 4.3-tahoe Never attribute to malice what can adequately be explained by incompetence. To Unsubscribe: send mail to [EMAIL PROTECTED] with "unsubscribe freebsd-current" in the body of the message