Bug 26767 - Optimization level -O3 changes the program behavior
Summary: Optimization level -O3 changes the program behavior
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2006-03-20 09:58 UTC by Gopal Mor
Modified: 2006-03-22 05:25 UTC (History)
56 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
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;

typedef struct _mixedInfo {
        airInfo_t    ai;
        atmosphere_t atm;

#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