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)05/31 Report--
This article mainly explains "how to calculate the memory alignment and size of C language structures". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "how to calculate the memory alignment and size of C language structures" together.
Structure memory alignment 1. Compute structure size struct S1{ char c1; // 1 byte, the default alignment is 8, so the alignment of c1 is 1, the first member variable is placed at an address offset from the struct variable 0 int i; // 4 byte, the default alignment number is 8, so the alignment number of i is 4, so i should be placed at an address with an offset of an integer multiple of 4 char c2; // 1 byte, the default alignment number is 8, so the alignment number of c2 is 1, so c2 should be placed at an address with an offset of an integer multiple of 1. //Maximum alignment is 4 //member size is 9, not an integer multiple of the maximum alignment number of 4, so the total struct size is 12};printf("%d\n", sizeof(struct S1));
Principle analysis:
[Practice Questions]
//Exercise 1struct S2{ char c1; // 1 byte / 8, alignment number is 1, char c2; // 1 byte / 8, alignment number is 1, int i; // 4 byte / 8, alignment number is 4, //Maximum alignment is 4 //The member size is 8, which is an integer multiple of the maximum alignment, so the total struct size is 8};printf("%d\n", sizeof(struct S2));//Exercise 2struct S3{ char c; // 1/8, alignment is 1 int i; // 4/8, alignment is 4 double d; // 8/8, alignment number is 8 //Maximum alignment is 8 //member size is 16, which is an integer multiple of the maximum alignment, so the total struct size is 16};printf("%d\n", sizeof(struct S3));//Exercise 3-struct nesting problem struct S4{ char c1; // 1/8, alignment is 1 struct S3 s3; // 16 byte,[the maximum alignment number of S3 is 8], so s3 should be placed at an address that is an integer multiple of 8 double d; // 8/8, alignment is 8 //Maximum alignment is 8 //The member size is 32, which is an integer multiple of the maximum alignment number, so the total size of the structure is 32};printf("%d\n", sizeof(struct S4));2. The alignment rules of the structure
The first member variable is at an address offset 0 from the struct variable.
Other member variables are aligned to addresses that are integer multiples of a number (alignment number).
Alignment = the smaller of the compiler default alignment and the member size.
The default alignment number in VS is "8", there is no alignment number in Linux
The total size of the struct is an integer multiple of the maximum number of alignments (one for each member variable).
If nested structs are aligned to an integer multiple of their maximum alignment, the overall size of the struct is an integer multiple of all maximum alignments, including the alignment of nested structs.
3. Why is there memory alignment?
Most of the references say this:
Platform reason (migration reason): Not all hardware platforms can access arbitrary data at arbitrary addresses; some hardware platforms can only fetch certain types of data at certain addresses, otherwise they throw hardware exceptions. (Then we need to align the data to these accessible addresses)
Performance reasons: Data structures (especially stacks) should be aligned on natural boundaries as much as possible. The reason is that the processor needs two memory accesses to access misaligned memory, whereas aligned memory accesses require only one access.
4. Summary
Memory alignment of structs is the practice of trading space for time.
Thinking: When designing the structure, we have to meet the alignment and save space. How to do it:
Keep members who occupy a small space together as much as possible.
//For example: struct S1{ char c1; // 1/8, alignment number is 1 int i; // 4/8, alignment is 4 char c2; // 1/8, alignment number is 1 //Maximum alignment is 4 //member size is 9, not an integer multiple of the maximum alignment, so the total struct size is 12};struct S2{ char c1; // 1/8, alignment number is 1 char c2; // 1/8, alignment number is 1 int i; // 4/8, alignment is 4 //Maximum alignment is 4 //The member size is 8, which is an integer multiple of the maximum alignment number, so the total structure size is 8};
The members of S1 and S2 are identical, but there are some differences in the size of the space occupied by S1 and S2.
Thank you for reading, the above is "C language structure memory alignment and how to calculate the size" of the content, after the study of this article, I believe we have a deeper understanding of the C language structure memory alignment and how to calculate the size of this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.