[Bug c++/56123] Union bitfield corruption

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Jan 28 01:20:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56123

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|major                       |normal

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-01-28 01:20:38 UTC ---
You're making invalid assumptions about implementation-defined behaviour.


  struct { // Bits
    uint   signal_handler_index : 24;
    uint   unused1 : 24;
    uint   orbid_connection : 16;
  };

Here the bit-fields take up three "allocation units" i.e. sizeof the struct is
3*sizeof(uint)
I believe this is because the platform ABI says unused1 cannot straddle two
4-byte units.


  struct { // Bits
    uint   signal_handler_index : 24;
    uint   unused1 : 8;
    uint   unused2 : 16;
    uint   orbid_connection : 16;
  };

Here the first two bit-fields both fit into an int, and so do the last two, so
sizeof the struct is only 2*sizeof(uint)

http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Structures-unions-enumerations-and-bit_002dfields-implementation.html

Other compilers on the same platform do the same thing.



More information about the Gcc-bugs mailing list