Wireshark-dev: Re: [Wireshark-dev] C Bitfield types and alignment

From: Jeff Morriss <jeff.morriss.ws@xxxxxxxxx>
Date: Mon, 05 Dec 2011 21:22:42 -0500
On 12/05/2011 04:41 PM, Bill Meier wrote:
For example: Given
struct radiotap_override {
guint8 field;
guint align:4, size:4;
} aaa;

struct radiotap_override {
guint8 field;
guint8 align:4, size:4;
} bbb;

It turns out that:
sizeof(aaa) = 8
sizeof(bbb) = 2

Apparently the alignment of the start of the bitfield depends upon the
(largest ?) bitfield type;

In the 'aaa' structure the compiler will (normally) insert 3 bytes of padding between 'field' and the bitfields so that the guint is 4-byte aligned; improper alignment isn't allowed on some CPUs (like SPARC) and is slow (often/always? requiring 2 memory access cycles to read) on others.