Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 Nov 2002 13:57:11 +0100 (CET)
From:      Harti Brandt <brandt@fokus.gmd.de>
To:        TOMITA Yoshinori <yoshint@flab.fujitsu.co.jp>
Cc:        FreeBSD-current@FreeBSD.ORG
Subject:   Re: gcc 3.2.1 optimization bug ?
Message-ID:  <20021111135051.H11404-100000@beagle.fokus.gmd.de>
In-Reply-To: <ywlvg34jn21.fsf@cerberus.proc.flab.fujitsu.co.jp>

next in thread | previous in thread | raw e-mail | index | archive | help
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 majordomo@FreeBSD.org
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
              brandt@fokus.gmd.de, brandt@fokus.fhg.de


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20021111135051.H11404-100000>