This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Bug report: in g++, -O2 only
- To: Maurizio Loreti <loreti at pd dot infn dot it>
- Subject: Re: Bug report: in g++, -O2 only
- From: dvv at dvv dot ru (Dima Volodin)
- Date: Thu, 08 Mar 2001 21:43:01 GMT
- Cc: <gcc at gcc dot gnu dot org>
- Organization: Huh?
- References: <Pine.LNX.4.33.0103082122590.3104-100000@mlinux.pd.infn.it>
It's not a bug, it's a valid result per strict aliasing rules. Use
-fno-strict-aliasing if you absolutely have to do things like that.
Dima
On Thu, 8 Mar 2001 21:26:29 +0100 (CET), you wrote:
>Hello - I have seen discussions about errors induced by -O in the mailing
>list; I have one minimal program that produces wrong output. Here it is:
>
>#include <iostream>
>int main() {
> unsigned long i;
> unsigned short *p = reinterpret_cast<unsigned short *>(&i);
> *p++ = 0x1234;
> *p = 0x5678;
> switch (i) {
> case 0x12345678: std::cout << "bigendian\n"; break;
> case 0x56781234: std::cout << "littleendian\n"; break;
> default: std::cout << "???\n";
> }
> return 0;
>}
>
>The program is in foo.cxx. Here is what happens:
>
>MLO@mlinux 59 $ g++ -v
>Reading specs from /usr/local/gcc/lib/gcc-lib/i686-pc-linux-gnu/2.97/specs
>Configured with: ../gcc-20010205/configure --prefix=/usr/local/gcc
>--enable-libstdcxx-v3
>gcc version 2.97 20010205 (experimental)
>MLO@mlinux 60 $ g++ -ansi -Wall -o foo foo.cxx && ./foo
>littleendian
>MLO@mlinux 61 $ g++ -ansi -Wall -O -o foo foo.cxx && ./foo
>littleendian
>MLO@mlinux 62 $ g++ -ansi -Wall -O2 -o foo foo.cxx && ./foo
>???
>
>I hope that the above bug report will be useful in your developement of
>g++.
>
>--
>Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
>Univ. of Padova, Dept. of Physics - Padova, Italy loreti@pd.infn.it
>
>