当前位置:网站首页>Detailed explanation of type, user-defined type, preliminary understanding of structure
Detailed explanation of type, user-defined type, preliminary understanding of structure
2022-07-19 08:10:00 【Qingfeng jade bone ( ω・)「 well】
Catalog
Definition, initialization and use of structure variables
Change the default alignment number
The cross platform problem of bit segment
Structure
stay C Language in , Structure (struct) It means a data structure , yes C In language Composite data class
(aggregate data type) One kind of .. A structure can be declared as Variable 、 The pointer or Array etc. , To achieve a more complex data structure .. A structure is also a collection of elements , These elements are called members of the structure (member), And these members can be of different types , Members usually visit by name
Statement of structure
// Statement of structure
struct Stu{
// Relevant attributes of students
char name[20];
int age;
char sex;
}s1,s2; // Created here s1、s2 Global variable
int main() {
struct Stu s3; //s3 It's a local variable
return 0;
}We can create the type first and create two global variables by the way s1、s2, You can also create local variables inside the function after the structure is created s3
Special statement
When you declare the structure , It can be stated incompletely
Anonymous struct type
struct // No name{int a;char b;float c;}x; // This is defining a Anonymous struct type
This naming method can only be used once , Because there's no name , So it won't be used in the future , This method can only be used when this type is used only once
struct
{
int a;
char b;
float c;
}x;
struct
{
int a;
char b;
float c;
}a[20], * p; // You can create a pointer to a structure , The type of pointer is the content defined aboveint main() { // When anonymous structures are used in this way, the compiler will warn
p = &x; // The compiler will treat the above two declarations as two completely different types , Even if their content is
return 0; // Same . So this behavior is illegal
}
Self reference of structure
// Self reference of structure
struct Node
{
int data; // Data fields
struct Node* next; // Pointer to the domain // Find nodes of the same type through pointers
};Use nodes to connect them in series , Self reference of structure , You need to include a structure pointer of the same type
We can use typedef To rename the structure type , Since the simple code
typedef struct Node
{
int data;
struct Node* next;
}Node;In this way, when using this type in the future, you can directly Node s1 To create a structural variable ;
This method and struct Node s1 It is equivalent.
Definition, initialization and use of structure variables
// Structure variable definition and initialization
#include<stdio.h>
struct Point
{
int x;
int y;
char name[20];
}p1 = { 2,3,"zhangshan" };
// We can initialize directly after creating the structure type
int main() {
struct Point p2 = { 4,5,"lisi" };
printf("%d %d %s\n", p1.x, p1.y, p1.name);
printf("%d %d %s\n", p2.x, p2.y, p2.name);
return 0;
}
// You can also create and initialize later 
Structure memory alignment
This is an examination site
// Structure memory alignment
#include<stdio.h>
// practice 1
struct S1
{
char c1;
int i;
char c2;
};
// practice 2
struct S2
{
char c1;
char c2;
int i;
};
int main() {
printf("%d\n", sizeof(struct S1));
printf("%d\n", sizeof(struct S2));
return 0;
}Let's first look at the result of this string of code

We will find that these two seem to be the same , There are two different results
computing method
First, you have to master the alignment rules of the structure :1. The first member is offset from the structure variable by 0 The address of .2. Other member variables are aligned to a number ( Align numbers ) An integral multiple of the address of .Align numbers = Compiler default alignment number And The size of the member Smaller value .VS The default value in is 83. The total size of the structure is the maximum number of alignments ( Each member variable has an alignment number ) Integer multiple .4. If the structure is nested , The nested structure is aligned to an integral multiple of its maximum alignment , Structure of the The overall size is the maximum number of alignments ( The number of alignments with nested structures ) Integer multiple .
analysis

Verification method
Here we can verify by offset , We make use of <stddef.h> Library functions in offsetof macro
#include<stdio.h>
#include<stddef.h>
// practice 1
struct S1
{
char c1;
int i;
char c2;
};
// practice 2
struct S2
{
char c1;
char c2;
int i;
};
int main() {
printf("%d\n", sizeof(struct S1)); //12
printf("%d\n\n", sizeof(struct S2)); //8
printf("%d\n", offsetof(struct S1,c1));
printf("%d\n", offsetof(struct S1,i));
printf("%d\n", offsetof(struct S1,c2));
return 0;
}result

Two exercises
// practice 3 ---16
struct S3
{
double d;
char c;
int i;
};
// practice 4- Structure nesting problem ---32
struct S4
{
char c1;
struct S3 s3;
double d; // When calculating the nested structure, you should carefully look at the fourth calculation method
};
Why is there memory alignment
1. Platform reasons ( Reasons for transplantation ):Not all hardware platforms can access any data on any address ; Some hardware platforms can only access certain types of data at certain addresses , 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 ; The aligned memory access only needs one access .
On the whole :
The memory alignment of the structure is Space In exchange for Time How to do it
Graphic analysis

When designing structures , We have to satisfy the alignment , And save space , Writing like this can be done
Let the members who occupy less space gather together as much as possible
// for example :
struct S1
{
char c1;
int i;
char c2;
};
struct S2
{
char c1;
char c2;
int i;
};Change the default alignment number
Before the change
// Change the default alignment number
#include<stdio.h>
struct S
{
int i;
double d;
};
int main() {
printf("%d\n", sizeof(struct S));
return 0;
}result

After modification
// Change the default alignment number
#include<stdio.h>
#pragma pack(4)
struct S
{
int i;
double d;
};
#pragma pack()
int main() {
printf("%d\n", sizeof(struct S));
return 0;
} 
If you don't want to align numbers like this 4 Change to 1 That's it , However, the default alignment number is generally 2^x Easy for the compiler to read data
Structural parameters
// Structural parameters
#include<stdio.h>
struct S
{
int data[1000];
int num;
};
void print1(struct S s)
{
for (int i = 0; i < 3; i++)
{
printf("%d ", s.data[i]);
}
printf("%d\n", s.num);
}
void print2(const struct S* ps) // prevent ps hold s changes
{
for (int i = 0; i < 3; i++)
{
printf("%d ", ps->data[i]);
}
printf("%d\n", ps->num);
}
int main() {
struct S s = { {1,2,3},100 };
print1(s); // Pass value
print2(&s); // Byref
return 0;
}
There are two methods of value transmission and address transmission , We usually use address transmission method
reason :
When a function passes parameters , The parameter is stack pressing , There will be time and space overhead .If you pass a structure object , The structure is too large , The system overhead of parameter stack pressing is relatively large , So it can lead to performance degradation .
Bit segment
What is a bit segment
The declaration and structure of a bit segment are similar , There are two differences :1. The member of the segment must be int、unsigned int or signed int .2. The member name of the segment is followed by a colon and a number .
// Bit segment
#include<stdio.h>
struct A {
int _a : 2; // Let it only occupy 2 individual bit, The same goes for the following // Sometimes we need a number to indicate true or false ,0 or 1
int _b : 5;
int _c : 10; // The following number is the number of bytes reopened for it // Then we only need one byte
int _d : 30;
};
//47bit
//6byte -- 48bit
//8byte -- 64bit
int main() {
printf("%d\n", sizeof(struct A)); // The result is 8
return 0;
}This method can save space to a certain extent
Bit segment memory allocation
1. The members of a segment can be int unsigned int signed int Or is it char ( It belongs to the plastic family ) type2. The space of the bit segment is based on 4 Bytes ( int ) perhaps 1 Bytes ( char ) The way to open up .3. Bit segments involve many uncertainties , Bit segments are not cross platform , Pay attention to portable program should avoid using bit segment .
Bit segment analysis
// An example
struct S {
char a : 3;
char b : 4;
char c : 5;
char d : 4;
};
int main() {
struct S s = { 0 };
s.a = 10;
s.b = 12;
s.c = 3;
s.d = 4;
return 0;
}

The cross platform problem of bit segment
1. int It's uncertain whether a bit segment is treated as a signed number or an unsigned number .2. The number of the largest bits in the bit segment cannot be determined .(16 The machine is the largest 16,32 The machine is the largest 32, It's written in 27, stay 16 position The machine will go wrong .3. The members in the bit segment are allocated from left to right in memory , Or right to left allocation criteria have not yet been defined .vs Middle is from right to left4. When a structure contains two bit segments , The second segment is relatively large , Cannot hold the remaining bits of the first bit segment , Whether to discard the remaining bits or to use , This is uncertain .
summary :Compared to the structure , Bit segments can achieve the same effect , But it can save a lot of space , But there are cross platform problems
Afterword
, I've been lazy recently
边栏推荐
- standard-version(发版与 Changelog 自动化)
- High performance integrated video image processing board based on ultrascale FPGA + Huawei Hisilicon arm / fpga+arm
- 2022 review questions and mock exams for main principals of hazardous chemical business units
- [MySQL] lock mechanism: detailed explanation of lock classification, table lock, row lock and page lock in InnoDB engine
- Redis 跳跃表实现原理 & 时间复杂度分析
- redis缓存雪崩、穿透、击穿
- Complete square number
- Go语言圣经
- Export file or download file
- FMC sub card: 4-way sfp+ 10 Gigabit optical fiber network FMC sub card
猜你喜欢
![[C language] user defined type details: structure, enumeration, union](/img/5c/69d238a71e24b34c2232c20ed7e09e.png)
[C language] user defined type details: structure, enumeration, union

Object detection and bounding box

redis6新功能

JS array intersection, subtraction and union

Jira --- workflow call external api
[characteristic Engineering]

半导体材料技术

From the casino logic, analyze the investment value of platform currency 2020-03-03

How did "leek" give money to "sickle"? 2020-03-07

【Kernel】驱动开发学习之字符设备
随机推荐
[MySQL] lock mechanism: detailed explanation of lock classification, table lock, row lock and page lock in InnoDB engine
Download, installation and use of mongodb
Understand LSTM and Gru
Junit5
Use of fiddler packet capturing tool
【Kernel】驱动开发学习之字符设备
Pytoch notes (3)
【flask入门系列】异常处理
Niuke topic - house raiding 1, house raiding 2
在线问题反馈模块实战(五):实现对通用字段内容自动填充功能
C语言一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
How to write the highlights of SCI papers (seriously teach you how to write)
【C语言】自定义类型详解:结构体、枚举、联合
Look back at the paper after reading the code: yolov3 reread
Forecast sales xgboost
What if the user information in the website app database is leaked and tampered with
养老年金保险有必要买吗?适合老人的养老年金产品有哪些?
Export file or download file
Virtual machine stack of [JVM]
看完代码回首看论文:YOLOv3重读