structural morphology
Structure: a structure is a collection of values called member variables. Each member of the structure can be a different type of variable.
Declaration of structure
struct Student { char name[20]; //full name char sex[2]; //Gender int age; //Age char colleage[20]; //school char clazz[20]; //class };
Definition and initialization of variables of structure
struct Point { int x; int y; }p1; struct Point p2; struct Point p3 = { 1,2 }; struct Node { int data; struct Point p; //Nested structure struct Node* next; }n1 = { 1,{1,3},NULL }; struct Node n2 = { 4,{5,
Structure memory alignment
Rules for calculating memory alignment of structures:
- The first member is at an address offset 0 from the structure variable.
- Other member variables should be aligned to the address of an integer multiple of a number (alignment number).
- Alignment number = the compiler default alignment number and the smaller value of the member size.
1. The default value in VS is 8
2. The default value in Linux is 4 - The total size of the structure is an integer multiple of the maximum number of alignments (one for each member variable).
- If a structure is nested, the nested structure is aligned to an integer multiple of its maximum alignment number, and the overall size of the structure is an integer multiple of all the maximum alignment numbers (including the alignment number of nested structures).
struct S1 { char c1; int i; char c2; };
According to the following figure, the memory alignment number of this structure is: 12 (to be an integer multiple of the maximum alignment number)
struct S2 { char c1; char c2; int i; };
struct S3 { double d; char c; int i; };
struct S4 { char c1; struct S3 s3; double d; };
If you omit the alignment map, you should draw 32 grids. The author will spit blood.
Number of alignments: 32
- In addition; We can also modify the alignment number:
Use: #pragma pack() to set the number of alignments
#pragma pack(8) / / set the default alignment number to 8
#pragma pack() / / restore the default alignment number
Structural transmission parameters
#include<stdio.h> #include<stdlib.h> typedef struct S { int data[1000]; int num; }S; print(struct S tmp) { int i = 0; for (i = 0; i < 5; i++) { printf("%d ", tmp.data[i]); } printf("\n"); printf("num=%d", tmp.num); } int main() { S s = { {1,2,3,4,5,6},8 }; print(s); return 0; }
Structural position segment
- Usage rules of bit segments:
1. The member of bit field must be int, unsigned int or signed int
2. There is a colon and a number after the member name of the bit field.
struct A{ int a:2; //Occupy two bit s int b=5; //5 bit s int c=10; //10 bit s int d=30; // 30 bit s }
4 bytes = 32 bits
And a + B + C = 17 < 32; You can share the space occupied by an int, and the number of bits of d is 30. You can only enjoy an int space. Therefore, the space occupied by this structure is 8 bytes
struct S { char a:3; //Occupy three bit char b:4; //Occupy four bit s char c:5; //Occupy five bit s char d:4; //Occupy four bit s }; struct S s = {0}; s.a = 10; s.b = 12; s.c = 3; s.d = 4;
a: Binary number of 10: 1010
b: Binary number of 12: 1100
c: Binary number of 3: 011
d: Binary number of 4: 100
Consortium
Definition of consortium:
Union is also a special user-defined type. The variables defined by this type also contain a series of members, which are characterized by the fact that these members share the same space (so
Consortium (also known as community).
Calculation of consortium size:
- The size of the union is at least the size of the largest member.
- When the maximum member size is not an integer multiple of the maximum alignment number, it should be aligned to an integer multiple of the maximum alignment number.
Statement of the consortium:
union un { char c; int i; };
Judging the size end mode of computer storage by Consortium
#include<stdio.h> #include<stdlib.h> int checkSystem() { union U { char c; int i; }u; u.i = 1; //When returning 1, it is the small end //When returning 0, it is the big end return u.c; } int main() { if (checkSystem == 1) { printf("Small end\n"); } else { printf("Big end\n"); } return 0; }