In the write-up The Lost Art of Structure Packing, the author introduces struct foo6 (...) in chapter 6 :
struct foo6 {
short s;
char c;
int flip:1;
int nybble:4;
int septet:7;
};
They explain that the padding is done as followed :
struct foo6 {
short s; /* 2 bytes */
char c; /* 1 byte */
int flip:1; /* total 1 bit */
int nybble:4; /* total 5 bits */
int pad1:3; /* pad to an 8-bit boundary */
int septet:7; /* 7 bits */
int pad2:25; /* pad to 32 bits */
};
But I don't get why the last bit field is padded to 32 bits. Following the previous explanations, I would have guessed a padding to 16 bits, because the strictest alignment condition is 2 bytes, for the short element. By padding 9 bits to the septet, the struct can be properly aligned on even addresses.
I have a feeling it has to do with the fact that it is a 32 bits system (as in sizeof int == 4) but it is not consistent with the previous explanations.
I have compiled printed the sizeof (struct foo6) with -m32 and -m64, and the result is indeed 8, not 6. But I still don't understand why.
First of all - it is hard to understand which struct as you use the same names to make it unclear. Assuming not packing
foo6total 8 bytes
foo7total 8 bytes