In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of the method of structure and memory alignment in C language, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on the method of structure and memory alignment in C language. Let's take a look.
1. Structure type
There are two types in C language: native type and custom type, and the structure type is a custom type.
two。 When using a structure, define the structure type first, and then use the type to define the variable.
When defining a structure, you need to define the structure type first, and then use the type to define the variable.
-> you can also define structure variables as well as structure types.
/ / define the type struct people {char name [20]; int age;}; / / define the variable while defining the type. Struct student {char name [20]; int age;} S1; / / renaming the type struct student to S1 maxim S1 is a type name, not a variable typedef struct student {char name [20]; int age;} S1 3. Progress from arrays to structures
-> structures can be thought of as evolving from arrays.
The-> array has two obvious drawbacks: the first is that the size must be clearly defined and cannot be changed later; the second is that the array requires that all elements must be of the same type.
The-> structure is used to solve the second defect of the array, which can be understood as an array in which the element types can be different.
4. How do I access the elements in the structure variable?
-> access to elements in an array: on the surface, there are two ways (array subscript and pointer); in fact, they are all accessed by pointer.
-> element access in structure variables: there is only one way to access elements in structure variables. Or-> to access.
Struct score {int a; int b; int c;}; struct myStruct {int a; / / 4 double b; / / 8 char c;}; int main () {struct myStruct S1; s 1.a = 12; / / int * p = (int *) & S1; * p = 12; s 1.b = 4.4; / / double * p = (double *) (& S1 + 4); * p = 4.4; s 1.c = "a" / / char * p = (char *) ((int) & S1 + 12); * p = "a"; int a [3]; / / 3 student scores, array score s; / 3 student grades, structural mode s a = 12; / / the compiler is internally converted to pointer access int * p = s; * (pair0) = 12; s.b = 44; / / int * p = s * (pendant 1) = 44; s.c = 64; / / int * p = s; * (pendant 2) = 44;} 5. Aligned access to structures
What is structure-aligned access:
/ / define a structure struct s {char c; / c actually occupies 4 bytes instead of 1 byte int b; int main (void) {struct s S1; s1.c = "t"; s1.b = 12; char * p1 = (char *) (& S1); printf ("* p1 =% c.", * p1); / / t int * p2 = (int *) ((int) & S1 + 1) Printf ("* p2 =% d.", * p2); / / 201852036. Get a strange number int * p3 = (int *) ((int) & S1 + 4); printf ("* p3 =% d.", * p3); / 12. Printf ("sizeof (struct s) =% d.", sizeof (struct s)); the result is 8 return 0;} 6. Why do structures need to be accessed in alignment
The main reason for the element alignment access in the-> structure is to match the hardware, that is, the hardware itself has physical restrictions. Alignment and access will improve efficiency, otherwise it will greatly reduce efficiency.
-> compare aligned access with unaligned access: aligned access sacrifices memory space for speed performance, while unaligned access sacrifices access speed performance for full utilization of memory space.
7. Structure alignment instance struct mystruct1 {/ / 1 byte alignment 4 byte alignment int a; / 4 4 char b; / 1 2 (1 # 1) short c; / / 22}; int main () {printf ("sizeof (struct mystruct1) =% d.", sizeof (struct mystruct1)); / / 8 return 0;}
Analysis: first of all, the whole structure, the whole structure variable 4-byte alignment is guaranteed by the compiler, we do not have to worry about. The start address of the first element, aQuery a, is the start address of the entire structure, so it is naturally 4-byte aligned. But the end address of an is determined by the next element. The second element b, because the previous element an itself occupies 4 bytes, is itself aligned. So the starting address left for b is also a 4-byte alignment address, so b can be put directly. After the start address of b is determined, the end address cannot be determined (because it may need to be populated), and the end address depends on the next element. Then there is the third element, cmagentshort type, which requires 2-byte alignment. (short type elements must be placed at an address like 0Magne2 and 4, not at an odd address like 1, so c cannot be stored next to B. the solution is to add a 1-byte fill after b, and then start putting c. C is not finished after playing, and when all the elements of the whole structure are aligned and stored, it is not over yet, because the size of the whole structure is also an integer multiple of 4.
Typedef struct mystruct2 {/ / 1 byte alignment 4 byte alignment char a; / 1 4 (1 # 3) int b; / / 4 4 short c / / 24 (2x2)} MyS2; int main () {printf ("sizeof (struct mystruct2) =% d.", sizeof (struct mystruct2)); / / 12 return 0;}
Struct mystruct1 {/ / 1 byte alignment 4 byte alignment int a; / 4 4 char b; / 1 2 (1 # 1) short c; / / 2 2}; typedef struct myStruct5 {/ / 1 byte alignment 4 byte alignment int a; / 4 4 struct mystruct1 S1; / / 7 8 double b / / 8 8 int c; / / 4 4} MyS5; int main () {printf ("sizeof (struct mystruct5) =% d.", sizeof (MyS5)); / / 24 return 0;}
Struct stu {/ / 1 byte alignment 4 byte alignment char sex; / / 1 4 (1 # 3) int length; / / 4 4 char name [10]; / 10 12 (10 # 2)}; int main () {printf ("sizeof (struct stu) =% d.", sizeof (struct stu)); / / 20 return 0;}
This is the end of the article on "how to align structures with memory in C language". Thank you for reading! I believe you all have some knowledge about "the method of structure and memory alignment in C language". If you want to learn more, 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.