On Mon, 11 Nov 2002, TOMITA Yoshinori wrote:

This is probably not a bug, but a feature. You are not expected to access
a variable through a pointer to a non-compatible type. int and short are
not compatible. (see your ISO C standard on this topic).

Try to use ntohl(), htonl() for your problem.

harti

TY>For the source code below, compiling gcc -O2/-O3 seem to produce
TY>incorrect code.
TY>
TY>-----------------------------------
TY>#include <stdio.h>
TY>int main(int argc, char* argv[])
TY>{
TY>    unsigned int x = 0x12345678;
TY>    unsigned short tmp;
TY>    printf("%x\n", x);
TY>    tmp = ((unsigned short *)&x)[0];
TY>    ((unsigned short *)&x)[0] = ((unsigned short *)&x)[1];
TY>    ((unsigned short *)&x)[1] = tmp;
TY>    printf("%x\n", x);
TY>    return 0;
TY>}
TY>-----------------------------------
TY>
TY>
TY>% gcc -O1 a.c
TY>% ./a.out
TY>12345678
TY>56781234
TY>
TY>% gcc -O2 a.c
TY>% ./a.out
TY>12345678
TY>12341234
TY>
TY>% gcc -v
TY>Using built-in specs.
TY>Configured with: FreeBSD/i386 system compiler
TY>Thread model: posix
TY>gcc version 3.2.1 [FreeBSD] 20021009 (prerelease)
TY>
TY>
TY>
TY>
TY>Is this a gcc bug or the source code problem ?
TY>
TY>To Unsubscribe: send mail to [EMAIL PROTECTED]
TY>with "unsubscribe freebsd-current" in the body of the message
TY>

-- 
harti brandt, http://www.fokus.gmd.de/research/cc/cats/employees/hartmut.brandt/private
              [EMAIL PROTECTED], [EMAIL PROTECTED]


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-current" in the body of the message

Reply via email to