In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the C++ structure byte alignment and common body size example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article. Next, let the editor take you to understand it.
1. Internal storage alignment of the structure
The alignment of the structure is often asked in written tests and interviews, so make a summary
Verify the memory size of different structures through code:
# include struct Node1 {char C1; int val1; char c2;}; struct Node2 {char C1; char c2; int val1;}; struct Node3 {char C1; char array [10];}; struct Node4 {char val1; int arrar [10];}; int main () {printf ("Node1 size =% d\ n", sizeof (struct Node1); printf ("Node2 size =% d\ n", sizeof (struct Node2) Printf ("Node3 size =% d\ n", sizeof (struct Node3)); printf ("Node4 size =% d\ n", sizeof (struct Node4)); return 0;}
The result of running the code is:
From the results of the above code, we can see that Node1 and Node2 define the same number of variables, but the size of Node1 is 12 and the size of Node2 is 8. Why?
First of all, two concepts are clear: the alignment number and the maximum alignment number. In the structure, the alignment number is the size of each member type. For example, in Node1, the alignment number is {1 ~ 4 ~ 1}. In the array, the alignment number is not the size of the array, but the size of the array members, so the alignment number of Node3 is {1 ~ # ~ 1}, and the alignment number of Node4 is {1 ~ # ~ 4}. The maximum number of alignments is the maximum of the number of alignments (gcc compiler), the maximum number of alignments may be affected by the compiler, usually the compiler will have the number of compiler alignments, the maximum number of alignments should be the smaller of the number of compiler alignments and the maximum number of alignments of structures, such as VS compiler is 8, if the maximum number of alignments of structures is 16, then the maximum number of alignment of computational structures should be 8. My compiler is gcc, so the maximum number of alignments is the maximum number of struct alignments.
Once you know the maximum alignment number, you can calculate the size of the structure, and you need to know that the size of the structure must be an integer multiple of the maximum alignment number. So Node1 and Node2 have the same member type, so why is the size of Node1 12 bytes and Node2 8 bytes? This is because of the continuity of memory in the structure, when the storage capacity is not up to the maximum number of memory, as long as the member can be saved, the structure will save the member variable in the memory space of the maximum alignment tree. This avoids excessive waste of memory.
Therefore, the memory size of the above structures is calculated as follows:
Sizeof (Node1) = 1 + 3 (waste) + 4 + 1 + 3 (waste) = 12
Sizeof (Node2) = 1 + 1 + 2 (waste) + 4 = 8
Sizeof (Node3) = 1 + 1 * 10 = 11
Sizeof (Node4) = 1 + 3 (waste) + 4 * 10 = 44
So how should the size of the nested structure be calculated? Give the following examples:
# include struct Node1 {char C1; int val1; char c2;}; struct Node2 {char C1; struct Node1 node; double val1;}; int main () {printf ("Node1 size =% d\ n", sizeof (struct Node1)); printf ("Node2 size =% d\ n", sizeof (struct Node2)); return 0;}
The result of the code running is:
It can be made clear that the alignment number of the nested structure is the maximum alignment number of the nested structure, so the alignment number of Node1 is {1 ~ (- 1), the alignment number of Node2 is {1 ~ (th), and the maximum alignment number is 4 and 8, respectively, the size of the two structures in the code is calculated as follows:
Sizeof (Node1) = 1 + 3 (waste) + 4 + 1 + 3 (waste) = 12
Sizeof (Node2) = 1 + 7 (waste) + 12 + 4 (waste) + 8 = 24
2. Memory size of the common body
For the following common body, the code to read its size is as follows:
# include union un1 {int val; char c; double d;}; union un2 {int val; char array [5];}; int main () {printf ("un1 size =% d\ n", sizeof (union un1)); printf ("un2 size =% d\ n", sizeof (union un2)); return 0;}
The result of running the code is:
The reason why a common body is called a common body is that its member variables share memory. Since it shares memory, the memory occupied by the common body must be able to hold the largest member type of memory, while the maximum memory member of un1 is flexible, with a size of 8 bytes, so the size of un1 is 8 bytes, so why is the memory size of un2 not 5? This is because memory alignment is required, and the common body also follows the memory alignment principle. The maximum alignment number of un2 is 4, so the size of un2 should be an integer multiple of 4, so sizeof (un2) = 8.
3. The size of the enumeration
Here, by the way, mention the memory size of the enumeration. The code verification is as follows:
# include enum Colour {RED, GREEN, BLUE}; enum ProgramLanguage {python = 0xffffffffff, c = 8, java}; int main () {printf ("Colour size =% d\ n", sizeof (enum Colour)); printf ("ProgramLanguage size =% d\ n", sizeof (enum ProgramLanguage)); return 0;}
The result of running the code is:
It can be seen that the size of the enumerated type is given by the compiler itself according to the defined value, and rarely exceeds the 4-byte size in actual use.
Thank you for reading this article carefully. I hope the article "sample analysis of byte alignment and common body size of C++ structures" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.