Consider the structure below, where the sum of bitfield sizes are 64-bit. Why does sizeof
say this structure is 12 bytes, when it should be 8?
typedef struct wl_Ls {
unsigned int total:17;
unsigned int used:17;
unsigned int entrySize:17;
_Bool point:1;
} wl_Ls;
[SOLUTION:] Using a 64-bit type fixes for first 2 or 3 members fixes it. Explanation is in the answer marked as Solution
Bitfields are not always guaranteed to be packed tightly together. Two of the situations where the compiler is allowed to insert padding between bitfields are: when two consecutive bitfields are not the same type, and when a bitfield doesn't fit into the number of bits that are still available in an "allocation unit" of the bitfield's type.
Assuming unsigned int
is 32 bits, all three pairs of consecutive bitfields in your structure qualify for at least one of those situations.