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
--
Summary: Optimization level -O3 changes the program behavior
Product: gcc
Version: 3.4.3
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: mor_gopal at yahoo dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26767