Here's what C and C++ compilers must do to keep structure members aligned.
As another example, suppose that gadget is a structure defined as:
struct gadget
{
char m1;
double m2;
char m3;
};
and that objects of type double must be double-word aligned. The compiler will add 7 bytes of padding after member m1 and also after member m3, as if the structure had been defined as:
struct gadget
{
char m1;
char padding_after_m1[7];
double m2;
char m3;
char padding_after_m3[7];
};
The most strictly aligned member of gadget is m2, which is double-word aligned. Thus, each gadget must also be double-word aligned. The padding after m1 ensures that member m2 is double-word aligned relative to the beginning of each gadget. The padding after m3 ensures that the next gadget in an array of gadgets is also double-word aligned.