Structures are packed to the size of "biggest word" used. Eg, if you have such structure:
struct ThreeBytes {
char one;
short two;
};
Its size will be 4 bytes, because field one
will be padded to the size of short, i.e. there is unused byte after that filed. If two
would be an int
, the structure will have size of two int
s. This happens if you align your structure to that:
// this structure got size of 4 bytes.
struct ThreeBytes {
char one;
char two;
short three;
};
And this is unaligned one:
// This structure will have size 6
struct ThreeBytes {
char one;
short two;
char three;
};
This is default behavior, there are compiler directives that allow change packing (see #pragma pack
, for example, compiler means may be different). Essentially you can set the unit to which fields will be padded or disable padding by setting it to 1. But some platforms do not allow that at all.