The size of a structure is depending on the alignment of the members, this can be influenced by the compiler default settings, or by explicit setting alignment (e.g. #pragma pack() for VisualC++
http://msdn.microsoft.com/en-us/library/2e70t5y1.aspx[
^]). To ensure this alignment padding is added (which are unused bytes that take up space and increase the size required to store the structure.
The padding is used to avoid inefficient access to data structures that are larger than a single byte.
Here an example of significant difference in size due to suboptimal ordering of members:
(char is 1 byte, long is 4 bytes and the alignment is optimized for 4 byte single access)
1 byte char1
3 byte <padding>
4 byte long1
1 byte char2
3 byte <padding>
4 byte long2
1 byte char3
3 byte <padding>
4 byte long3
1 byte char4
3 byte <padding>
4 byte long4
The whole structure was 32 bytes due to the necessary padding to align the longs to the next 4 byte boundary.
Reordering the members cut this down to 20 bytes:
4 byte long1
4 byte long2
4 byte long3
4 byte long4
1 byte char1
1 byte char2
1 byte char3
1 byte char4