In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
What is the custom type in C language? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Structure size
Let's first give a random structure, and in order to calculate its size, I give a complete printing scheme:
Typedef struct num {char c; int n; char cc;} num;int main () {printf ("% d\ n", sizeof (num)); return 0;}
Well, according to reason, I calculate the size of a structure depends on how much space its members consume. The three members of the num structure are char, int, and char types, corresponding to 1, 4, 1-byte size, so it only takes 6 bytes to ok; but-- let's take a look at the print results:
Forget it when you say a little bit of error, the good guy just skewed it twice as much, why? To explain this problem, we need to introduce offsetof.
Offsetof
Offsetof is essentially a macro. The C library macro offsetof generates an integer constant of type size_t (size_t is defined in the standard C library and is long long unsigned int in 64-bit systems), which is the byte offset of a structure member relative to the beginning of the structure. Declared as:
Offsetof (type, member-designator)
The member of the structure is given by member-designator, and the name of the structure is given in type, which needs header file support.
Let's come to Kangkang, what is the offset of each member?
# includeint main () {printf ("% d\ n", offsetof (num,c)); printf ("% d\ n", offsetof (num, n)); printf ("% d\ n", offsetof (num, cc)); return 0;}
The results are as follows:
We can clearly see how the composition of 12 just came into being. We know that the offset units are bytes, and our 0meme4 and 8 are three offset units, that is, bytes. The offset of the position where num begins to be stored in memory relative to the position of the first member is 0, that is, at the same location, the first byte offset is 0, the second byte offset is 1, and the third byte offset is 2. and so on.
Let's create a picture to illustrate this process (hand-disabled ppt)
C _ ccc is an one-byte char type, and n is a four-byte int type, so in fact the space we use is only the colored part above.
So the new question is, why is there a blank part (the offset is 1Magne2, 3 and the unpainted 12)? What happened to the blank part? Then we need to understand the alignment rules of structures.
Structure alignment rule
1. The first member of the structure exists at the address where the offset of the structure is 0, that is, from the same starting point.
two。 Other member variables are aligned to the address of an integer multiple of a number (alignment). The number of addresses is equal to the smaller value of the default alignment number of the compiler and the size of the member (the compiler I use is vs 2019 vs. the default value is 8, but there is no default alignment number in the Linux environment, the alignment number is the member's own size)
3. The total size of the structure is an integral multiple of the maximum alignment.
4. If the structure is nested, the nested structure is aligned to an integer multiple of its maximum alignment, and the overall size of the structure is an integral multiple of all the maximum alignment.
Explanation:
The first rule is easy to understand. Let's take member n as an example. The size of n is 4. The compiler default alignment number is 8, and the smaller value 4 in 4Power8 is taken as the alignment number.
How to understand the maximum alignment of Article 3? In fact, it is the one with the largest number of alignment among all the members, the alignment number of the three members is 1Magi 4, and the maximum value is 4. If it is an integral multiple of 4, we happen to take the last member, the cc alignment number is 8. The size of the whole space 0-8 is a multiple of 9, not 4, we throw away, and then continue to waste three spaces until we come to our 12, meet the conditions to jump out.
Take a chestnut:
Struct num2 {double dtchar cittint n;}
We draw his memory distribution according to the rules:
Because the double type is 8 bytes, as the largest member, the alignment is 8, from 0-15 to 16, 16 is an integer multiple of 8, so the structure size is 16. The nesting situation is not detailed, which is the same as the general situation.
Reason of existence
= = So, why does the structure have this alignment mechanism? = = two aspects:
From the perspective of portability: different platforms have different functions, not all hardware platforms can access any data on any address, and some hardware platforms can only get specific data types on specific addresses, otherwise hardware exceptions will occur.
From a performance point of view: data structures, especially stacks, should be aligned from the natural boundary as much as possible, because in order to access memory, the processor needs to access the scattered space twice, while aligned memory only needs to be done once. that's what we usually do: trade space for time.
If the structural alignment is not appropriate, can we change the default alignment to improve performance? Yes, of course!
# pragma pack (num) {. Return 0;} # pragma pack ()
This macro can cancel the default alignment, set the num in parentheses to the alignment number you are satisfied with, and then cancel your own settings with this macro.
After reading the above, have you mastered the method of customizing types in C language? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.