Briefly talk about your understanding of structure knowledge


Note: The IDE used by the author is Microsoft Visual Studio 2022 (64-bit platform)

1.Introduction

Why do we need a structure in the first place?
Answer: In order to represent some complex things, but ordinary basic types cannot meet the actual requirements, such as personal information from name, age, gender, phone number, home address, etc. If you want to input it in batches, then compared with a single variable (structure (called members within the body, and will be replaced by members in the future) is difficult to manage. To organize all these members into a whole, first of all, each internal member serves the structure, which has a certain logic. Secondly, from the structure Visiting various internal members is also more reader-friendly.
What is a structure?
Answer: A new data type is formed by combining some basic types of data.

2. Creation of structure type 1

2.1 General statement

Go directly to code, such as describing an employee

struct employee//结构体类型名
{
    
    
	char name[10];//姓名
	int age;//年龄
	char sex[10];//性别
	char phone[20]//电话号码
}e1//结构体变量名称;

2.2 Special statement

Or go directly to the code?

struct //匿名结构体
{
    
    
	char name[10];//姓名
	int age;//年龄
	char sex[10];//性别
	char phone[20]//电话号码
}e1//结构体变量名称;

This kind of anonymous structure type can only be used once if the structure type is not renamed.

2.3 Self-reference of structure

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

Anonymous structures should not be defined when the structure needs to be self-referential.

3. Structure initialization and access

3.1 Initialization of structure

Directly upload the code

#include <stdio.h>
struct employee
{
    
    
	char name[10];
	int age;
	char sex[10];
	char phone[20]}e1;
int main()
{
    
    
	struct employee e1 = {
    
     "wangwu",18,"male","12312312312" };//初始化
	return 0;
}

3.2 Structure member access

结构体变量.成员变量名
结构体指针—>成员变量名

Example

#include <stdio.h>
struct employee
{
    
    
	char name[10];
	int age;
	char sex[10];
	char phone[20];
}e1;
int main()
{
    
    
	struct employee e1 = {
    
     "wangwu",18,"male","12312312312" };
	e1.age = 20;
	printf("%d \n", e1.age);
	struct employee* e2 = &e1;//定义结构体指针并赋值
	e2->age = 25;//结构体指针访问成员
	printf("%d \n", e1.age);
	printf("%d \n", e2->age);
	return 0;
}

4. Structure memory alignment

4.1 Structure size

Let’s start with the code

#include <stdio.h>
struct employee
{
    
    
	char name[4];
	int age;
	char sex[5];
	char phone[4];
}e1;
int main()
{
    
    
	printf("%d \n", sizeof(e1));
	return 0;
}

When we run the calculation employee size, it shows 20. If the structure size is calculated literally, it should be name(4)+age(4)+sex(5)+phone(4)=17 bytes. So the calculation of the structure size is not the calculation we usually understand.

4.2 Memory alignment

First, you must master the alignment rules of structures 1 :

1.结构体的第⼀个成员对⻬到相对结构体变量起始位置偏移量为0的地址处
2.其他成员变量要对⻬到某个数字(对⻬数)的整数倍的地址处。
对⻬数=编译器默认的⼀个对⻬数与该成员变量⼤⼩的较⼩值。
- VS中默认的值为8 
-Linux中没有默认对⻬数,对⻬数就是成员⾃⾝的⼤⼩
3.结构体总⼤⼩为最⼤对⻬数(结构体中每个成员变量都有⼀个对⻬数,所有对⻬数中最⼤的)的整数倍。
4.如果嵌套了结构体的情况,嵌套的结构体成员对⻬到⾃⼰的成员中最⼤对⻬数的整数倍处,结构体的整体⼤⼩就是所有最⼤对⻬数(含嵌套结构体中成员的对⻬数)的整数倍

To sum up, we recalculate the size of emplpyee

#include <stdio.h>
//VS默认对齐数为8
struct employee
{
    
    
	char name[4];//4个字节 开辟空间占位0~3
	int age; //4个字节 开辟空间占位4~7
	char sex[5]; //5个字节 开辟空间占位8~12
	char phone[4];//4个字节 开辟空间占位13~16
	//一共占位0-16个字节,共17个字节。而最大对齐数为4跟8较小的数,就是4。
	//所以结构体整体大小就是4的整数倍也就是20
}e1;
int main()
{
    
    
	printf("%d \n", sizeof(e1));
	return 0;
}

4.3 The significance of memory alignment 1

1. 平台原因(移植原因):
不是所有的硬件平台都能访问任意地址上的任意数据的;某些硬件平台只能在某些地址处取某些特定
类型的数据,否则抛出硬件异常。
2. 性能原因:
数据结构(尤其是栈)应该尽可能地在⾃然边界上对⻬。原因在于,为了访问未对⻬的内存,处理器需要
作两次内存访问;⽽对⻬的内存访问仅需要⼀次访问。假设⼀个处理器总是从内存中取8个字节,则地
址必须是8的倍数。如果我们能保证将所有的double类型的数据的地址都对⻬成8的倍数,那么就可以
⽤⼀个内存操作来读或者写值了。否则,我们可能需要执⾏两次内存访问,因为对象可能被分放在两
个8字节内存块中。
总体来说:结构体的内存对⻬是拿空间来换取时间的做法

  1. Refer to the Bit Employment Courseware "Customized Type: Structure" ↩︎ ↩︎ ↩︎

Guess you like

Origin blog.csdn.net/BlankXiangzw/article/details/133023133