In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to use C language # pragma pack (n) to set byte alignment, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
# pragma pack (n)
This is the parameter setting for the compiler. With regard to the structure byte alignment setting, # pragma pack specifies how the data is aligned in memory.
# pragma pack (n) function: the C compiler will align according to n bytes.
# pragma pack () function: cancel custom byte alignment.
# pragma pack (push,1) function: means to set the original alignment to stack and set the new alignment to one byte alignment
# pragma pack (pop) function: restore alignment
Therefore, it can be seen that the addition of push and pop can restore the alignment to its original state, rather than the compiler default, which can be said to be better, but in many cases there is little difference between the two.
Such as:
# pragma pack (push) / / Save alignment
# pragma pack (4) / / set to 4-byte alignment
Equivalent to # pragma pack (push,4)
Explanation 1:
Compilers on each particular platform have their own default "alignment factor" (also known as alignment modulus). Programmers can change this coefficient by precompiling the commands # pragma pack (n), nasty 1, 2, 4, 8, 16, where n is the "alignment factor" you want to specify.
Rules:
1. Data member alignment rule: the data member of the structure (struct) (or union). The first data member is placed where the offset is 0, and then each data member is aligned according to the value specified by # pragma pack and the length of the data member, the smaller one.
2. The overall alignment rule of the structure (or union): after the data members have completed their respective alignment, the structure (or union) itself will be aligned according to the value specified by # pragma pack and the smaller one of the length of the most big data member of the structure (or union).
Explanation 2:
N-byte alignment VC's special handling of structural storage does improve the speed of CPU to store variables, but sometimes it also brings some trouble. We also mask the default alignment of variables, and we can set the alignment of variables ourselves. # pragma pack (n) is provided in VC to set variables to n-byte alignment. N-byte alignment means that there are two cases of offset of the starting address where the variable is stored:
First, if n is greater than or equal to the number of bytes occupied by the variable, then the offset must satisfy the default alignment.
Second, if n is less than the number of bytes occupied by the type of the variable, the offset is a multiple of n and does not have to satisfy the default alignment. There is also a constraint on the total size of the structure, which is divided into the following two cases: if n is greater than the number of bytes occupied by all member variable types, then the total size of the structure must be a multiple of the space occupied by the variable that takes up the largest space; otherwise, it must be a multiple of n.
The following examples illustrate its usage. # pragma pack (push) / / Save alignment
# pragma pack (4) / / set to 4-byte alignment
Struct test {char M1; double M4; int M3;}; # pragma pack (pop) / / the size of the above structures is 16:
The following analysis of its storage situation, the first allocation of space for M1, its offset is 0, to meet our own set alignment (4-byte alignment), M1 size is 1 byte. Then start allocating space for M4, where the offset is 1, which requires 3 bytes, so that the offset satisfies a multiple of sizeof 4 (because sizeof (double) is greater than 4), and M4 takes up 8 bytes. Then space is allocated for m3, where the offset is 12, which is a multiple of 4, and m3 takes up 4 bytes. At this point, space has been allocated for all member variables, with a total of 16 bytes, which is a multiple of n. If we change the above # pragma pack (4) to # pragma pack (8), we can get that the size of the structure is 24.
You must be numb when you read these descriptions. After I insisted on reading it, I wrote a program by myself:
# pragma pack (4) struct node {int e; char f; short int a; char b;}; struct node nscape printf ("% d\ n", sizeof (n))
My own calculation is 16, and the actual result is:
twelve
Then change the location of the data members inside the structure:
# pragma pack (4) struct node {char f; int e; short int a; char b;}; struct node nscape printf ("% d\ n", sizeof (n)); 12
Force positioning 2 for the number of aligned bits
# pragma pack (2) struct node {char f; int e; short int a; char b;}; struct node nscape printf ("% d\ n", sizeof (n)); 10
Force positioning 1 for the number of aligned bits
# pragma pack (1) struct node {char f; int e; short int a; char b;}; struct node nscape printf ("% d\ n", sizeof (n)); 8
Looking at the output and the text description is a little dizzy, the following is a brief description of my decision rules:
In fact, the reason why there is a memory byte alignment mechanism is to minimize the number of memory reads. We know that CPU read speed is at least one order of magnitude faster than memory read speed, so in order to save computing time, we have to sacrifice space in exchange for time.
The following example shows how to minimize the number of reads.
# pragma pack (1) struct node {char f; int e; short int a; char b;}; struct node nscape printf ("% d\ n", sizeof (n))
Here, it is forced to be aligned according to 1 byte, which can be understood as that all the contents are read according to 1 byte (for a moment, because this is a good understanding of the mechanism of memory). All other data members are integer multiples of 1 byte, so there is no need for memory alignment, each member is arranged according to the actual order in memory, and the actual length of the structure is 8.
# pragma pack (2) struct node {char f; int e; short int a; char b;}; struct node nscape printf ("% d\ n", sizeof (n))
Alignment is forced to 2 bytes here. If the memory distribution is still continuous, then int e has to be read into CPU three times, so in order to "pay attention" to the reading of int e, reserve 1BYTE after char f, and the last char b is the same, so the length is 10
# pragma pack (4) struct node {char f; int e; short int a; char b;}; struct node nscape printf ("% d\ n", sizeof (n))
Alignment is forced to 4 bytes here. So reserve 3BYTE after char f, and short int an and char b can read to CPU at once (read by 4 bytes), so the length is 12.
If the n in # pramga pack (n) is greater than the number of bytes occupied by any one of the structure members, the n value is invalid. The compiler will select the number of bytes of the most big data member in the structure as the benchmark.
The above is how to use # pragma pack (n) of C language to set byte alignment. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.