Take the following struct:
struct s {
char c; // 1 byte
long l; // 8 bytes
};
C would add 7 bytes of padding between c
and l
, making the size of the struct 16 and the alignment of the struct 8. If I told the compiler to pack the struct, how is the alignment evaluated? Does it just give up and make the alignment 1 byte?
EDIT:
This question is not a duplicate of Struct Padding and Packing. I am asking how the alignment of a packed struct is evaluated. Not how padding is done or how the alignment of a regular struct is evaluated. If the above struct is padded, it has a size of 16 and an alignment of 8--the alignment of its largest member (the long
). If it is packed, it has a size of 9 bytes and an alignment of ? bytes.