Advanced structure

Posted by YOUAREtehSCENE on Sun, 06 Mar 2022 17:17:27 +0100

catalogue

1. Basic knowledgeDeclaration of structure

2. Structure self reference

3. Initialization of structure

4. Structure memory alignment

5. Modify the default alignment number

1. Basic knowledge

The values of some variables with different structures can be members of different types.

Declaration of structure

struct tag//tag can be customized
{
    member-list;

}variable-list;

For example, we can describe a student:

Declaring a structure type is to create a student variable (object) through the student type.

The characteristics of a student include name, gender, age, telephone number and so on.

struct stu
{
    char name[20];
    char tele[12];
    char sex[10];
    int age;
};//struct is the structure keyword, stu is the structure label, and you can take any name.

struct s
{
	char name[20];
	int age;
};
int main()
{
	struct s arr[50];//It can store 50 struct s type data
	return 0;
}

We create structure variables as follows:

struct stu
{
	char name[20];
	char tele[12];
	char sex[10];
	int age;
}s4,s5,s6;//Yes, S3, S4, S5 and S6 are all global variables
struct stu s3;//global variable
int main()
{
	//Creation of structure variables
	struct stu s1;
	struct stu s2;
	return 0;
}

Sometimes we may not have a label after the structure type, so this is an anonymous structure.

Anonymous structure type. There is no structure label after struct. You can only use the variable list to create variable x

//struct
//{
//	int a;
//	char b;
//	float c;
//}x;

//struct
//{
//	int a;
//	char c;
//}x;

//struct
//{
//	int a;
//	char c;
//}* pas;// The anonymous structure pointer type cannot be used to store the address of x, because it is a completely different type
 Anonymous struct types can only be used once
//int main()
//{
//
//	return 0;
//}

2. Structure self reference

You cannot include your own type in the structure, such as 1.1

typedef struct Node

{

int date;

// 1.1struct Node n;//error: structure type cannot contain member variables of its own type

struct Node *n;// Correct self reference mode of structure

}Node;

int main()

{

struct Node n1;

Node n2;// There is no problem with both

return 0;

}

3. Initialization of structure

Let's start with a piece of code

struct t
{
	double weight;
	short age;
};
struct s
{
	char c;
	struct T st;
	int a;
	double d;
	char arr[20];
};
int main()
{
	struct s s = {'c',{55.6,30},100,3.14,"hello world"};
	printf("%c,%d,%lf,%s\n",s.c,s.a,s.d,s.arr);//Access to structure members
	printf("%lf\n",s.st.weight);
	return 0;
}

When initializing the structure, we use "{}", such as the code block above:

struct s s = {'c',{50.6,30},100,3,14,"hello world"}

This statement creates a structure variable s to assign a value to the structure. Note that the assigned values should be separated by commas.

When accessing structural variables, we need to use structural variables (s) + (.) For example, the first printf statement above.

4. Structure memory alignment

Learning memory alignment will help us calculate the size of structures and study how to minimize structures to save space.

struct s1
{
	char c1;
	int a;
	char c2;
};
struct s2
{
	char c1;
	char c2;
	int a;
};
int main()
{
	struct s1 s1 = {0};
	printf("%d\n",sizeof(s1));
	struct s2 s2 = {0};
	printf("%d\n",sizeof(s2));
	return 0;
}

In this code, the size of s1 is 12 bytes and the size of s2 is 8 bytes.

To understand why we define the same variable but have different structure sizes, we must understand the structure memory alignment rules.

1. The first member is at the address offset from the structure variable by 0.

2. Other member variables should be aligned at the address of an integer multiple of a number (alignment number).

3. The total size of the structure is an integer multiple of the maximum alignment number (each member variable has an alignment number).

4. If a structure is nested, the nested structure is aligned to an integer multiple of its maximum alignment number,

The overall size of a structure is an integer multiple of the maximum number of alignments (including the number of alignments of nested structures).

Alignment number = the smaller value of the compiler's default alignment number and the size of the member. The VS compiler defaults to 8

Let me explain to you what these two sentences mean by drawing pictures.

 

So the space of s1 is 12 bytes.

Next, let's study how to calculate the structure size when the structure is nested.

struct s3
{
	double d;//Alignment number 8
	char c;//Alignment number 1
	char i;//Alignment number 1
};//The total size of the structure is 16
struct s4
{
	char c1;//Alignment number 1
	struct s3;//Maximum number of alignments 8
	int i;//Alignment number 4
};

 

So s4 size is 32.

Therefore, we should put the variables of small stores occupying space in front as much as possible.

 5. Modify the default number of alignments

#pragma pack(4) / / set the default alignment number to 4.

#pragma pack() / / cancel the default alignment number.

struct s
{
    char c;
    int i;
    double d;
};
int main()
{
    offsetof()//Judge the offset of the member from the starting position
}

 

Topics: C Back-end