Typically, compilers will insert padding to align members to boundaries that match their size. Here 16-bits of padding would be inserted between a
and b
to ensure b
is 32-bit aligned.
int16_t a;
/* 16 bits of padding */
int32_t b;
But if the struct members would naturally fill that space, there's no need to insert padding. Here each member is already aligned: a
and b
are 16-bit aligned and c
is 32-bit aligned.
int16_t a;
int16_t b;
int32_t c;
Standardizing each variable to 32-bits would waste space.
Similarly, you could have a mix of variable sizes. Inserting a small amount of padding is more efficient than expanding all of the variables to a large uniform size.
int8_t a;
/* 8 bits of padding */
int16_t b;
int32_t c;