Oversize bitfields

Mark Mitchell mark at codesourcery.com
Wed Feb 23 00:46:39 UTC 2000


I don't quite understand:

  When a bitfield member of a class is specified with a size greater
  than the field type declared, e.g. char c: 32, it is allocated as
  follows. The field alignment used is that of the largest integer type
  that will fit in the given size, and the first bits of the field
  (low-order for little-endian IA-64) are used to contain the actual
  data.

What does the `field alignment' mean here?  Do we mean that given:

  struct S { char c1 : 33; char c2: 33 };

that this structure will take up 16 bytes?  (The biggest type to fit
in either field is an `int', which has 4-byte size/alignment.)  So, if
we want to 4-byte align the fields, we put them at offsets 0 and 8,
respectively.  That's slightly in contrast to:

  struct S { short s1 : 15; short s2 : 1 };

where we use only two bytes for `S'; we don't try to align `s2' on a
`short' boundary.  

I understand the rationale: we might store a whole `int' in each
field, and then the larger alignment will win.  I'm just trying to be
sure I understand.

How about the following wording:

  Let n be the number of bits in the bitfield.  Let T be the largest
  integer type that will fit in n bits.  For the purposes of class
  layout, treat the field as if it were a non-bitfield of type `T',
  followed by n - 8 * sizeof (T) bits of padding.

That expresses nicely the idea that we want the field to be aligned as
if for a `T'.

--
Mark Mitchell                   mark at codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com




More information about the cxx-abi-dev mailing list