Alignment rules in C ++
What is alignment?
In C/C ++, the member variables of data structures or classes are not arranged one by one in a compact manner based on their sizes. They are arranged according to a specific method and may insert one or more bytes between two member variables, to ensure that the starting position of each member variable starts from a specific position. This is alignment. It is difficult to understand alignment simply in terms of language. Examples of alignment in the following sections describe the alignment rules of C/C ++.
Why alignment?
On most platforms, the system reads data very quickly from some specific locations, while reading data from other locations is much slower. C/C ++ is a language that focuses on efficiency. In order to make the program as fast as possible, we choose to sacrifice a small amount of space, the byte filling method ensures that all data storage starts from these specific locations and achieves a high running speed.
What is 4-byte alignment?
When it comes to the n-byte alignment of a struct, it contains two pieces of information:
(1) The starting address of the struct can be divisible by n.
(2) The total size of the struct can be divisible by n
When a member variable is n Bytes aligned, it indicates that the starting address of the variable can be divisible by n. For example, if the number of aligned bytes of a variable is 4, The hexadecimal format of its first address must end with 0/4/8/C.
In actual scenarios, n can be 1, 2, 4, or 8.
Rules are starting.
1. In 64-bit systems, the default alignment byte is 8. In the 32-bit system, the information found on the internet is 4 or 8, because the 64-bit system is used and the 32-bit system has not been verified.
struct type{ int a; long b;};int main(){ type t; cout<
Running result:
size = 16address a = 0x7fffa0b9e9d0address b = 0x7fffa0b9e9d8
Arrangement:
A A A A 0 0 0 0
B B B B B B B B
2. For the entire struct, the entire size must also be a multiple of the number of bytes. If not, it will be supplemented later.
struct type{ long a; int b;};int main(){ type t; cout<
Running result:
size = 16address a = 0x7ffff8bb0840address b = 0x7ffff8bb0848
Arrangement:
A A A A A A A A
B B B B 0 0 0 0
3. You can use # pragma pack to modify the default value, but you can only change it to a smaller value.
#pragma pack (4)struct type{ long a; int b;};int main(){ type t; cout<
Running result:
size = 12address a = 0x7fffe365d4d0address b = 0x7fffe365d4d8
Arrangement:
A A A A
A A A A
B B B B
4.
For a struct or a member variable in a class, its alignment is performed according to the value specified by # pragma pack and the smaller member length.
A = automatic length of data members
B = default value; value range: 1, 2, 4, 8
This data member alignment byte = min (a, B)
struct type{ bool a; int b;};int main(){ type t; cout<
Running result:
size = 8address a = 0x7fff43062ed0address b = 0x7fff43062ed4
Arrangement:
A 0 0 0
B B B B
5.
For a struct, the alignment byte is determined by the largest member in the structure and the value specified by # pragma pack.
A = automatic length of the maximum data member in the Structure
B = default value; value range: 1, 2, 4, 8
This struct alignment byte = min (a, B)
struct type{ int a; bool b;};int main(){ type t; cout<
Running result:
size = 8address a = 0x7fff518d64a0address b = 0x7fff518d64a4
Arrangement:
A A A A
B 0 0 0
6.
When the default value is equal to or greater than the length of all data members, the default value does not have any effect.
A = max (length of all data members)
B = default value; value range: 1, 2, 4, 8
If (B>)
This struct alignment number of bytes is
7. struct containing struct
If a member variable is another struct, the alignment bytes of the member variable are the alignment bytes of the struct.
Example 1:
struct byte3{ char a; char b; char c;};struct type{ byte3 a; byte3 b; byte3 c;};int main(){ type t; cout<
Running result
size = 9address a = 0x7fff6346cdf0address b = 0x7fff6346cdf3address c = 0x7fff6346cdf6
Note:
Because the maximum length of all struct is 1, it is 1-byte aligned. Although the size of a struct is 3, it is still aligned with 1.
Example 2:
struct byte3{ short a; char b;};struct type{ byte3 a; byte3 b; byte3 c;};int main(){ type t; cout<
Running result:
size = 12address a = 0x7fffff3f1ef0address b = 0x7fffff3f1ef4address c = 0x7fffff3f1ef8
Note:
Since the maximum length of all structs is 1, they are 2-byte aligned.