C language structure byte alignment

Copy the text from this article: https://www.cnblogs.com/sunbines/p/9257981.html

Byte aligned reasons:

1) Platform reason (transplant reason): Not all hardware platforms can access any data on any address, some hardware platforms can take only certain types of data at a certain address, or hardware exception is thrown
2) hardware reasons: after the memory alignment, memory access speed CPU greatly enhanced.

Using directive #pragma pack (n), C compiler aligned in n bytes.
Using directive #pragma pack (), to cancel the custom byte alignment.


Byte alignment rules:

Maximum base data type member 1. The divisible structure of the overall size of the can.
2. The structure member address minus the first address of the structure that is called the offset value, the offset value must be divisible by member variable size.

Summary: #pragma pack when the value of n is equal to or exceeds the length of all the data members, the size of the value of n will not produce any effect.

Natural alignment: If the address of a variable store an integer multiple of the data size of the variable. Such as: int-type data storage address must be a multiple of 4, the data storage address of the short type, if a certain multiple of 2.

Guess you like

Origin blog.csdn.net/u013594490/article/details/93774986