title: A bug in GCC implementation of bit-fields
body: <p>Working in C11, the following struct:</p>
  
  <pre><code>struct S {
    unsigned a : 4;
    _Bool    b : 1;
  };
  </code></pre>
  
  <p>Gets layed out by GCC as an <code>unsigned</code> (4 bytes) of which 4 bits are used, followed by a <code>_Bool</code> (4 bytes) of which 1 bit is used, for a total size of 8 bytes.</p>
  
  <p>Note that C99 and C11 specifically permit <code>_Bool</code> as a bit-field member. The C11 standard (and probably C99 too) also states under §6.7.2.1 'Structure and union specifiers' ¶11 that:</p>
  
  <blockquote>
    <p>An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit.</p>
  </blockquote>
  
  <p>So I believe that the member <code>b</code> above should have been packed into the storage unit allocated for the member <code>a</code>, resulting in a struct of total size 4 bytes.</p>
  
  <p>GCC behaves correctly and packing does occur when using the same types for the two members, or when one is <code>unsigned</code> and the other <code>signed</code>, but the types <code>unsigned</code> and <code>_Bool</code> seem to be considered too distinct by GCC for it to handle them correctly.</p>
  
  <p>Can someone confirm my interpretation of the standard, and that this is indeed a GCC bug?</p>
  
  <p>I'm also interested in a work-around (some compiler switch, pragma, <code>__attribute__</code>...).</p>
  
  <p>I'm using gcc 4.7.0 with <code>-std=c11</code> (although other settings show the same behavior.)</p>