A structure member is accessed after adding the offset to the structure address. Since a small offset is advantageous for the size, members often used should be declared at the top.
The most effective combination is within 32 bytes from the top for the signed char or unsigned char type, within 64 bytes from the top for the short or unsigned short type, or within 128 bytes from the top for the int, unsigned int, long, or unsigned long type.
An example in which the code is changed because of the offset of the structure is shown below.
Source code before improvement
Assembly-language expansion code before improvement
Assembly-language expansion code after improvement
When defining a structure, declare the members while considering the boundary alignment value. The boundary alignment value of a structure is the most largest boundary alignment value within the structure. The size of a structure becomes a multiple of the boundary alignment value. For this reason, when the end of a structure does not match the boundary alignment value of the structure itself, the size of the structure also includes the unused area that was created for guaranteeing the next boundary alignment. |
Source code before improvement