NO

Author Topic: Boolean bit-field "alignment"  (Read 2313 times)

RichieL

  • Guest
Boolean bit-field "alignment"
« on: March 29, 2007, 12:05:44 PM »
This isn't a bug, since the resulting behavior is correct, but the size of a structure containing bit-fields that mix the types "unsigned int" and "bool" depends on the order of the fields. Specifically, it appears that LCC requires "bool" structure members to reside in the low byte of a four-byte aligned area. Using "unsigned int" instead of "bool" to represent single-bit quantities in the "middle" of a set of bit-fields will result in more compact structures.

Code snippet (compiled for x86):

Code: [Select]
  #include "stdbool.h"
  typedef unsigned int u32;
 
  struct  _first
    {
     bool   a  : 1;
     u32    b  : 15;
     u32    c  : 16;
    u32   d;
    } First;

  struct  _second
    {
     u32    c  : 16;
     bool   a  : 1;
     u32    b  : 15;
    u32   d;
    } Second;

  void main()
  {
      printf("First struct is %d bytes, second struct is %d bytes\n", sizeof(struct _first), sizeof (struct _second));
      exit(123);
  }
output result:
First struct is 8 bytes, second struct is 12 bytes
*** Process returned 123 ***