Wireshark mailing list archives

Re: C Bitfield types and alignment


From: Jeff Morriss <jeff.morriss.ws () gmail com>
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.
___________________________________________________________________________
Sent via:    Wireshark-dev mailing list <wireshark-dev () wireshark org>
Archives:    http://www.wireshark.org/lists/wireshark-dev
Unsubscribe: https://wireshark.org/mailman/options/wireshark-dev
            mailto:wireshark-dev-request () wireshark org?subject=unsubscribe


Current thread: