g++-4.0.3: program output is different when compiled with -O3 (did not happen with gcc-3.4.5)

Erik sigra@home.se
Sun May 14 13:21:00 GMT 2006


I just installed g++-4.0.3 on a Pentium Mobile. I have a program that 
behaves as expected when compiled with -O0 but gives different output 
when compiled with -O3. I do not know what to do about it. The program 
is called prov. Here is the result of compiling and running it:
$ g++-4.0.3 -O0 -Wall -o prov prov.cc -I/usr/include/SDL && ./prov; echo $?
1
$ g++-4.0.3 -O3 -Wall -o prov prov.cc -I/usr/include/SDL && ./prov; echo $?
0

The program looks like this:
______________________________________________________
#include <SDL_types.h>
struct Coords {
    bool operator<(const Coords other) const {
        return
            *reinterpret_cast<const Uint32 * const>(this)
            <
            *reinterpret_cast<const Uint32 * const>(&other);
    }
   
    int x : 16, y : 16;
};
int main() {
    Coords a = {0, 1}, b = {1, 0};
    return b < a and a < b;
}
______________________________________________________


The operator< is for use with standard containers, so it has to work. It 
should of course never happen that b is less than a and a is less than 
b, so the program should always return 0. I can see that the assembly 
output is very different, but I admitt that I do not understand much of 
it. With g++-3.4.5 the program produces identical output for the 
different optimization levels.



More information about the Gcc-help mailing list