当前位置:网站首页>Custom types: structural bodies
Custom types: structural bodies
2022-06-10 18:25:00 【some scenically beautiful place】
Catalog
Structure variable definition and initialization
Structure memory alignment structure transfer parameter structure
Structure
1. Declaration of a structure
1.1 Basic knowledge of
A structure is a complex type , An array is a collection of the same elements ;
Structs are also collections of values , Each member of a structure can be of a different type .
1.2 Structure declaration
Take a chestnut
// Take students as chestnuts , Create a structure variable
struct stu
{
int age;
float score[5];
char name[10];
char sex[5];
};// Semicolons cannot be omitted , Generally, the compiler will automatically add 1.3 Special structures
Anonymous struct type
struct // Structure labels are omitted
{
int a;
double b;
char c;
}tmp;This structure can only be used once
Two anonymous struct types , If one of them is &, Structure pointer , It is not clear whether it refers to ?
notes :
The compiler will treat two anonymous structures as different structure types , There will be no ambiguous reference .
1.4 Self reference of structure
struct node
{
int t;
struct node;
};
// Whether it is legal ?The answer is that such self - application is illegal .
introduce : data structure
The storage structure of data in memory :
The order sheet : Such as arrays
Linked list

Self references to structs include pointers of the same type
struct node
{
int t;
struct node* next;
};
Calculate its size sizeof(struct node) by 4+4/8;
1.5 Definition and initialization of structure variables
struct node
{
int t;
char c;
}p1;// Declare and agree p1
struct node p2;// Definition p2;
// initialization
struct node p1 = { 0,0 };
1.6 Structure memory alignment
Calculate the structure memory size
How to calculate ?
struct node
{
char c1;
int i;
char c2;
};
int main()
{
printf("%d\n",sizeof(struct node));
return 0;
}Running results :12

computing method :
1. The first member of the structure is placed in the memory storage location of the variable of the structure variable 0 Start at offset
2. All members from the second member are placed in the aligned number ( The smaller of the member size and the default alignment number ) An integral multiple of the address of
3. The total size of the structure is an integer multiple of the maximum number of member alignments
4. In the case of nested structures , The nested structure members are aligned to an integer multiple of their maximum alignment number , The overall size of the structure is an integral multiple of the maximum alignment number .
notes :
vs2019 The default number of alignments is 8

After the heap is completed, its size is 9bit, according to 3 The conclusion is that 12bit.
If you change the position of the above structure members :


The memory allocated by the computer has been reduced to 8bit
so : To reduce memory waste, you can put members of the same type together
The reason for the memory pair :
1) Platform reasons ( Transplant problem ):
Not all hardware platforms can access any data on any address ; Some hardware platforms can only take some special data at some addresses
Certain types of data , Otherwise, a hardware exception will be thrown .
2) Performance reasons :
data structure ( Especially stacks ) It should be aligned as far as possible on the natural boundary .
The reason lies in , To access unaligned memory , The processor needs to make two memory accesses ; Aligned memory access requires only one access
ask .
Space for time , Multiple variables can be accessed or extracted at once , Increase of efficiency .
Ways to reduce memory waste
1) Put variables of the same type together
2) Change the default alignment number
1.7 Change the default alignment number
Modify the default alignment number to #pragma Instructions
#pragma pack ( n ) Change the default alignment number
#include<stdio.h>
#pragma pack(8)//vs2019 The default number of alignments
struct
{
char c1;
int i;
char c2;
}s1;
#pragma pack()
#pragma pack(1)// Change the default alignment number to 1
struct
{
char c1;
int i;
char c2;
}s2;
#pragma pack()// Restore to the default alignment number
int main()
{
printf("%d\n",sizeof(s1));
printf("%d\n", sizeof(s2));
return 0;
}
Find the library function of the offset offsetof
http://www.cplusplus.com/reference/cstddef/offsetof/?kw=offsetof
1.8 Structural parameters
Similar to functions , There are value transmission and address transmission , Of course, addressing is a better solution
#include<stdio.h>
struct stu
{
int age;
char name[10];
};
struct stu s = { 10," Li Hua " };
void print1(struct stu s)
{
printf("%s\n", s.name);
}
void print2(struct stu* ps)
{
printf("%s\n", ps->name);
}
int main()
{
print1(s);// Pass value
print2(&s);// Byref
return 0;
}边栏推荐
- 小程序积分商城如何实现营销目的
- C language -- 14 loop statement for
- IP summary (tcp/ip volumes 1 and 2)
- Library for adding progress bar during training --tqdm
- 关于cmake和gcc的安装的记录
- .NET 开源的免费午餐结束了?
- c语言---10 初识结构体
- Summary of all contents of cloud computing setup to ensure that a complete cloud computing server can be built, including node installation, instance allocation, network configuration, etc
- flutter系列之:UI layout简介
- Cdga| six key points of data governance for industrial enterprises
猜你喜欢
随机推荐
js模糊阴影跟随动画js特效插件
AOV network topology sorting
The short ticket hypothesis: finding sparse, trainable neural networks
c语言---4 初识常量
c语言---9 初识宏、指针
Abbexa AML1 DNA 结合 ELISA 试剂盒说明书
CodeCraft-22 and Codeforces Round #795 (Div. 2)
c语言---2 初识数据类型
滑动窗口最值问题
三部曲套路解bob活命问题
如何定位游戏发热问题
【技术分析】探讨大世界游戏的制作流程及技术——前期流程篇
Canvas fire burning H5 animation JS special effects
【数据库】结构化数据、非结构化数据、半结构化数据的区别
Abbexa 细菌基因组 DNA 试剂盒介绍
CodeCraft-22 and Codeforces Round #795 (Div. 2)
苹果放大招!这件事干的太漂亮了……
After the qtmqtt source code compilation is set to keepalive, the Ping package timeout error does not return a problem repair (qmqtt:: mqttnopingresponse, qmqtt:: clientprivate:: onpingtimeo)
软考不通过能不能补考?解答来了
美学心得(第二百三十七集) 罗国正









