...
This is often referred to as structure padding. Structure members are arranged in memory as they are declared in the program text. Padding may be added to the structure to ensure the structure is properly aligned in memory. Structure padding allows for faster member access on many architectures.
Rearranging the fields in a struct
can change the size of the struct
. It is possible to minimize padding anomalies if the fields are arranged in such a way that fields of the same size are grouped together.
Padding is also referred to as "Struct Member Alignmentstruct
member alignment." Many compilers provide a flag that controls how the members of a structure are packed into memory. Modifying this flag may cause the size of the structures to vary. Most compilers also include a keyword that removes all padding; the resulting structures are referred to as packed structures. Overriding the default behavior is often unwise because it leads to interface compatibility problems (the nominally same struct
has its layout interpreted differently in different modules).
Non-Compliant Code Example
...