[C language] what you must know about structures

🚀 1, Structure

🌈 1.1 declaration of structure type

A structure is a collection of values, which are called member variables. Each member of a structure can be a variable of different types. So how to declare a structure?
For example, describe a student:

struct Stu
{
	char name[20];//name
	int age;//Age
	char sex[3];//Gender
	char id[20];//Student number
};//Don't forget the semicolon

There is also a declaration method, called anonymous structure type. This structure creates structure variables when declaring, and can only be created when declaring.

struct 
{
	int a;
	char b;
	double c;
}x;

🌈 1.2 self reference of structure

When we want to self reference a structure, we must not write this

struct Node
{
	int data;
	struct Node next;
};

In this way, the system cannot determine the size of the struct Node type after opening up the int type of 4 bytes, and will enter the next again. After opening up the shaping of 4 bytes, it will enter the next, forming an endless loop. The correct writing method should be as follows:

struct Node
{
	int data;
	struct Node *next;
};

In this way, if the size of * next and the size of structure are determined, there will be no program crash.

🌈 1.3 definition of structural variables

Generally, we can define structure variables while declaring the structure, or define after declaration:

struct Node
{
	int a;
	char b;
	double c;
}x;//The structure variable x is defined at the same time as the declaration
struct Node n;//Define structure variables after declaration

In C + +, we can directly declare the structure and then define the structure variables, such as Node x; Compared with C language, there is one less struct, which can also be abbreviated in C language. The method is to use the typedef keyword to define the structure as a type, and the type name is generally consistent with the structure name:

typedef struct Node
{
	int a;
	char b;
	double c;
}Node;
Node x;Directly define structure variables

🌈 1.4 initialization of structure variables

🛴 1.4.1 initialization during definition

struct Node p={1,'a',1.2};

This method must pay attention to the order and cannot be misplaced.

🛴 1.4.2 assign values one by one after definition

struct Node p;
p.a=1;
p.b='a';
p.c=1.2;

In this case of single assignment, pay attention not to write the variables wrong, and the order is not important.

🛴 1.4.3 out of order assignment during definition

struct Node p={.a=1,.b='a',.c=1.2};

In this way, there is no order. You can even initialize only one value, and the other values will be changed to 0 by default.

🛴 1.4.4 out of order assignment during definition (c + + style)

struct Node p={
	a:1,
	b:'a',
	c:1.2
};

This style, also known as c + + style, uses key value pairs, and the order is naturally not important.

🌈 1.5 structure memory alignment

Let's help me calculate the size of the following two structures

struct Node1
{
	int a;
	char b;
	char c;
};
struct Node2
{
	char b;
	int a;
	char c;
	
};

The first size is 8 bytes and the second size is 16 bytes. Why? Didn't you change the order and the result would be different? This is actually the result of structure memory alignment. You have mastered the structure alignment rules and calculated the structure size, such as finding things.

🛴 1.5.1 structure alignment rules

  1. The first member is at an address offset 0 from the structure variable.
  2. 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.
    The default value in VS is 8
  3. The total size of the structure is an integer multiple of the maximum number of alignments (one for each member variable).
  4. If a structure is nested, the nested structure is aligned to an integer multiple of its maximum alignment number, and the integer of the structure is zero
    The volume size is an integer multiple of the maximum number of alignments (including the number of alignments of nested structures).

🛴 1.5.2 why does structural alignment exist

1. Platform reason (migration reason): not all hardware platforms can access any data at any address; Some hardware platforms can only get certain types of data at certain addresses, otherwise hardware exceptions will be thrown.
2. Performance reason: data structures (especially stacks) should be aligned on natural boundaries as much as possible. The reason is that in order to access misaligned memory, the processor needs to make two memory accesses; Aligned memory access requires only one access.
The essence of structure alignment is to use space for time
Therefore, when we design the structure, we should try to gather the small space together

🛴 1.5.3 modify the default alignment number

As mentioned above, the default alignment number of vs is 8. We can modify the default alignment number through #pragma this preprocessing instruction

#pragma pack(4) / / modify the default alignment number to 4
#pragma pack() / / unset the default alignment number and restore it to the default

🛴 1.5.4 exercise for calculating the size of structure

Calculate the size of the structure. Many large factories love to work out this similar problem in the written examination. See if you have mastered it through the following examples:

//Exercise 1
struct S1
{
 char c1;
 int i;
 char c2;
};
printf("%d\n", sizeof(struct S1));

//Exercise 2
struct S2
{
 char c1;
 char c2;
 int i;
};
printf("%d\n", sizeof(struct S2));

//Exercise 3
struct S3
{
 double d;
 char c;
 int i;
};
printf("%d\n", sizeof(struct S3));

//Exercise 4 - Structural nesting problem
struct S4
{
 char c1;
 struct S3 s3;
 double d;
};
printf("%d\n", sizeof(struct S4));

The outputs are 16 8 16 32 in turn

Tags: C C++

Posted on Tue, 21 Sep 2021 06:00:01 -0400 by Kane250