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
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
*** This bug has been marked as a duplicate of 21920 ***
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