Detailed Explanation of C Language Structure Knowledge System

This article brings the knowledge about structures and the rules of structure memory alignment in detail!

If you think the article is good, I look forward to your one-click three-link. Your encouragement is the source of motivation for my creation. Let us work together, run together, and let us meet at the top! ! !

Table of contents

​edit

This article brings the knowledge about structures and the rules of structure memory alignment in detail!

If you think the article is good, I look forward to your one-click three-link. Your encouragement is the source of motivation for my creation. Let us work together, run together, and let us meet at the top! ! !

1. Know what is a structure

2. Declaration of structure

special structure declaration

​edit

Struct type renaming

3. Self-reference of structure

Common errors when anonymous structs are self-referencing: 

4. Initialization of structure variables

Initialization and printing of struct nested structs

Five. Structure memory alignment (the most important)

focus:

Alignment rules for structures:

macro offsetof

Example of struct nested struct

 Why does memory alignment exist?

Modify the default alignment

5. Structure parameter passing


1. Know what is a structure

A structure is a collection of values ​​called member variables. Each member of the structure can be a variable of a different type .
Here it is distinguished from an array, which is a collection of elements of the same type ;

2. Declaration of structure

 like:

tag is the structure tag name , which can be customized;
member-list is the list of members ;
variable-list is a list of variables ;
It should be noted that the following semicolon cannot be lost;

 here:

  struct Stu is a structure type;

  char name[ 20] , int age , char sex[5] are all member variables ;

  S1, S2, S3 are structure variables ;

There are two ways to create structure variables;

The first one: after the structure is declared first, then the structure variable (global variable) is created  on the left side of the figure;

The second type: when the structure is declared, the structure is created directly behind the brackets (it is a local variable)   on the right side of the figure;

special structure declaration

Note: Anonymous structure type ( incomplete declaration ), when creating a structure variable, only the first method above can be used; (as shown in the figure)

 The above two structures omit the structure tag ( tag ) when declaring;

So, here comes the question,    is p=&S  in the above code legal?

warn:
The compiler will treat the above two declarations as two completely different types.
So it's illegal.

Struct type renaming

The following code:

 The above code : rename the structure type struct Stu to Stu;

You can also rename the anonymous structure type, as in the following code:

 Therefore, when creating a structure variable, you can directly use Stu S1, S2, S3 to create it;

3. Self-reference of structure

Is it okay to have a member in a struct that is of type the struct itself?
The structure contains a structure (itself or another structure), and a structure pointer is needed;
The following code:

Common errors when anonymous structs are self-referencing: 

4. Initialization of structure variables

Notice:

Without the dot operator, it can only be initialized according to the order of the member list at the time of declaration, and the initial order can be changed by using the dot operator

Initialization and printing of struct nested structs

Structures are enclosed in braces, so when a structure contains another structure, the structure inside also needs to use braces;

Print the data in the above structure: for example, print the data of Sn2

Five. Structure memory alignment (the most important)

focus:

A particularly popular test point:     structure memory alignment
Inspection method:                 calculate the size of the structure

example:

But the actual running result is:

Why is this? This is about the alignment rules of structures;

Alignment rules for structures:

  1. The first member is at the address whose offset is 0 from the structure variable.
2. Other member variables should be aligned to an address that is an integer multiple of a certain number (alignment number).
Alignment = Compiler's default alignment and the smaller value of the member size.
The default value in VS is 8
There is no default alignment number in Linux, the alignment number is the size of the member itself
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, and the entire structure
The body size is an integer multiple of all maximum alignments (including those of nested structures). 
Then analyze the above code:

macro offsetof

1. It is used to calculate the offset of the structure members compared to the starting position of the structure;
2. The header file that offsetof needs to include is: stddef.h
We use this macro to validate our analysis:
The result of the operation is the same as that of our analysis and comparison above;

Example of struct nested struct

Print to verify our analysis:

 Why does memory alignment exist ?

1. Platform reason ( transplant reason ) :
Not all hardware platforms can access arbitrary data at any address; some hardware platforms can only fetch certain types , otherwise a hardware exception will be thrown.
2. Performance reasons :
Data structures ( especially stacks ) should be aligned on natural boundaries as much as possible.
The reason is that to access unaligned memory, the processor needs to make two memory accesses; while aligned memory access requires only one
ask.
In general:
The memory alignment of the structure is the practice of exchanging space for time .
When designing the structure, we must satisfy the alignment and save space. How to do it:
Let members who occupy a small space gather together as much as possible;

Modify the default alignment

#pragma This preprocessing directive can change our default alignment.
Example:

 in conclusion:

When the alignment of the structure is inappropriate, we can change the default alignment by ourselves.

5. Structure parameter passing

When a structure is passed as a parameter, the address of the structure must be passed.

 Which of the above print1 and print2 functions is better?

The answer is: the print2 function is preferred
reason:
When a function passes parameters, the parameters need to be pushed onto the stack, which will cause system overhead in time and space.
If a structure object is passed, the structure is too large, and the system overhead of pushing parameters on the stack is relatively large, which will lead to performance degradation.
decline.
in conclusion:
When a structure is passed as a parameter, the address of the structure must be passed.
end of this chapter~

Guess you like

Origin blog.csdn.net/2301_77509762/article/details/131959317