C language - custom type - structure (11.1)

Table of contents

mind Mapping:

1. Basic knowledge of structure types

1.1 Declaration of structure

1.2 Special notice

2. Self-reference of structure

3. Definition and initialization of structure variables

4. Structure memory alignment

4.1 How to calculate

4.2 How to modify the inner alignment number

5. Structure parameter passing

Write at the end:


mind Mapping:

1. Basic knowledge of structure types

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.

1.1 Declaration of structure

A structure needs to be declared before it can be used.

example:

struct Stu//这就是一个结构体声明
{
	char name[10];
	int age;
};

1.2 Special notice

example:

//匿名结构体类型
struct //声明时不写结构体名字
{
	char name[10];
	int age;
}x;//这种结构体只能使用一次

2. Self-reference of structure

Structures can refer to themselves.

example:

typedef struct Node//typedef将struct Node重命名为Node
{
	int data;
	struct Node* next;
}Node;

int main()
{
	Node s1;
	Node s2;
	s1.next = &s2;
	return 0;
}

3. Definition and initialization of structure variables

To use a structure, you need to create a structure variable.

example:

struct Stu
{
	char name[10];
	int age;
};

int main()
{
	struct Stu s1;//创建结构体变量s1
	              //这个是局部变量
	return 0;
}

Structs can also create global variables:

example:

struct Stu
{
	char name[10];
	int age;
}s1, s2;//创建结构体全局变量s1和s2

int main()
{
	return 0;
}

4. Structure memory alignment

The memory alignment of the structure is a very important knowledge point.

By learning the memory alignment of the struct, we can calculate the size of the struct.

4.1 How to calculate

example:

#include <stdio.h>

struct S1
{
	char c1;
	int i;
	char c2;
};

struct S2
{
	char c1;
	char c2;
	int i;
};

int main()
{
	printf("%d\n", sizeof(struct S1));
	printf("%d\n", sizeof(struct S2));
	return 0;
}

output:

输出:
12
8

The members of these two structures are exactly the same, but their positions are changed.

The size of the structure is completely different.

This is the result of the alignment within the structure.

Structures are stored in memory like this:

So the final structure occupies 12 bytes of memory space.

In the same way:

 So in the end the structure occupies 8 bytes of space.

practise:

#include <stdio.h>

struct S3
{
	double d;
	char c;
	int i;
};

int main()
{
	printf("%d\n", sizeof(struct S3));
	return 0;
}

output:

输出:16

Exercise 2:

Questions about structure nesting:

#include <stdio.h>

struct S3
{
	double d;//对齐数是8
	char c;
	int i;
};

struct S4
{
	char c1;
	struct S3 s3;//如果结构体嵌套,对齐数是嵌套的结构体的最大对齐数
	double d;                              //对于struct S3来说就是8
};

int main()
{
	printf("%d\n", sizeof(struct S4));
	return 0;
}

output:

输出:32

Summarize:

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 = the compiler's default alignment and the smaller value of the member size)

Note: The default alignment number of the VS environment is 8.

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 own maximum alignment number,

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

4.2 How to modify the inner alignment number

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

example:

#include <stdio.h>

#pragma pack(8)//设置默认对齐数为8

struct S1
{
	char c1;
	int i;
	char c2;
};

#pragma pack()//取消设置的默认对齐数,还原为默认
#pragma pack(1)//设置默认对齐数为1(就是不对齐了)

struct S2
{
	char c1;
	int i;
	char c2;
};

#pragma pack()//取消设置的默认对齐数,还原为默认

int main()
{
	printf("%d\n", sizeof(struct S1));
	printf("%d\n", sizeof(struct S2));
}

output:

输出:
12
6

Then why is there an alignment number?

There are two versions:

1. Platform reason (transplant reason):

Not all hardware platforms can access any data at any address,

Some hardware platforms can only fetch certain types of data at certain addresses, otherwise a hardware exception is thrown.

2. Performance reasons:

Data structures (especially stacks) should be aligned on natural boundaries as much as possible.

When accessing unaligned memory, the processor needs to make two memory accesses, while aligned memory access requires only one access.

5. Structure parameter passing

Go directly to the code:

example:

#include <stdio.h>

typedef struct Stu
{
	char name[10];
	int age;
}Stu;//用typedef将结构体重命名成Stu

void print(Stu* ps)
{
	printf("%s %d", ps->name, ps->age);
}

int main()
{
	Stu s1 = { "张三",18 };//创建结构体变量并初始化(赋值)
	Stu* ps = &s1;
	print(ps);//结构体也推荐传值调用
	return 0;
}

output:

输出;张三 18

When a function passes parameters, the parameters need to be pushed onto the stack, which will cause system overhead in time and space.

If when passing a structure object, the structure is too large,

The system overhead of parameter pushing is relatively large, so it will lead to a decrease in performance.

Summarize:

It is best to pass pointers when passing parameters to structures.

Write at the end:

The above is the content of this article, thank you for reading.

If you like this article, please like and comment, and write down your opinions.

If you want to learn programming with me, you might as well follow me, we will learn and grow together.

I will output more high-quality content in the future, welcome to watch.

Guess you like

Origin blog.csdn.net/Locky136/article/details/128306749