Bug 26767

Summary: Optimization level -O3 changes the program behavior
Product: gcc Reporter: Gopal Mor <mor_gopal>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: adruab, Andreas.Glowatz, andreg, bobm75, boris, c.pop, cdfrey, charles, christophe.guillon, cxl, czang, davids, davmac, devin, djk, dmeggy, duraid, evgeny, ezharkov, fabdouze, fm, gcc-bugs, gcc, ghouston, gino, gopalv82, grigory_zagorodnev, hayim, horst.lehser, hurbain, ivan, ja2morri, jason.elbaum, jfran, jochang, larschri, lindsayd, linuxadmin, lucho, micis, mike, mor_gopal, noaml, pgonzalez, pierre.chatelier, pjh, rarob, rick.ju, spelis, steffen.zimmermann, steger, strasbur, sumii, thomas.anders, warp, zybi
Priority: P3    
Version: 3.4.3   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Gopal Mor 2006-03-20 09:58:28 UTC
Following program generates different outputs for -O1 and -O3 optimization levels.

#include <stdio.h>

typedef struct _airInfo{
        unsigned int craftNo : 16;
        unsigned int onAir:1;
        unsigned int numCrew:2;
        unsigned int numPassenger:2;
        unsigned int onLand:1;
        unsigned int fuelTank:2;
        unsigned int height: 8;
} airInfo_t ;

#pragma pack(1)
typedef struct _atmosphere {
        int temp:24;
        int humidity:24;
        int pressure:24;
        int altitude:24;
}atmosphere_t;

typedef struct _mixedInfo {
        airInfo_t    ai;
        atmosphere_t atm;
}mixedInfo_t;

#pragma pack()

int main() {
        mixedInfo_t mi ;

        mi.ai.height       = 17;
        mi.ai.craftNo      = 1;
        mi.ai.onAir        = 0;
        mi.ai.numCrew      = 2;
        mi.ai.numPassenger = 2;
        mi.ai.onLand       = 0;
        mi.ai.fuelTank     = 0;

        printf("ai in num = %u\n", (*((unsigned int *)(&mi.ai)))) ;
        return 0 ;
}

// Output from program when compiled with -O1
$ g++ -O1 test1.cxx ; ./a.out 
ai in num = 286523393

// Output from program when compiled with -O3
$ g++ -O3 test1.cxx ; ./a.out 
ai in num = 1310720
Comment 1 Gopal Mor 2006-03-20 10:00:37 UTC
Following details are added.
// g++ version info
$ g++ -v
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.3 20050227 (Red Hat 3.4.3-22.1)

// System info
$uname -a
Linux NEW1_dev_opteron 2.6.9-11.EL #1 Fri May 20 18:15:25 EDT 2005 x86_64 x86_64 x86_64 GNU/Linux



Comment 2 Richard Biener 2006-03-20 10:08:14 UTC

*** This bug has been marked as a duplicate of 21920 ***
Comment 3 Gopal Mor 2006-03-22 05:25:57 UTC
The following keywords are added to this page, so that developers facing similar problem can search (hopefully) this page using search engines.

bitfield problem bug gcc g++ optimization bit field strict aliasing ISO C