当前位置:网站首页>Advanced part of C language IV. detailed explanation of user-defined types
Advanced part of C language IV. detailed explanation of user-defined types
2022-07-24 07:33:00 【And move forward with the high wind - & gt;】
Catalog
Structure variable definition and initialization
Structs implement bit segments ( Filling of bit segments & Portability )
Structure
Declaration of struct type
A collection of various data types of a structure , These data are called structure members . When you create a structure variable , Each member has their own space .
Declaration format :
Self reference of structure
Structure variable definition and initialization
There are many ways to define structure variables , for example
Structure memory alignment
When calculating the size of structure, we need to consider the memory alignment of structure , The reasons for the existence of structure memory alignment are as follows :
1. Platform reasons : Some hardware platforms can only access certain types of data at certain addresses , Otherwise, a hardware exception will be thrown .
2. Performance reasons : Accessing unaligned memory , The processor needs to access twice , The aligned memory is accessed only once
So the data structure ( Stack ) It should be aligned on natural boundaries as much as possible .
Memory alignment rules :
1. The first member is aligned to be offset from the structure variable by 0 The location of
2. Other members are aligned to an integer multiple of their respective alignment numbers ( Alignment number is the compiler default alignment number and member size ( Number of bytes ) The lesser of )
3. The total size of the structure is an integer multiple of the maximum alignment number of each member
4. If nested structures , The nested structure is aligned to an integer multiple of the maximum alignment number of its members , The total size of the structure is the maximum alignment number of all members ( Members with nested structures ) Integer multiple
VS2022 The compiler's default alignment number is 8, use #pragma pack(4) You can modify the default alignment number of the compiler to 4
Structural parameters
Structure parameters are usually passed to the address of the structure , Structure pointer , This saves memory overhead , Because the structural parameters need to be pressed , When the structure is large, the system time and space overhead are large .
Structs implement bit segments ( Filling of bit segments & Portability )
Bit segment is a special type of structure ,( Bit segments are not cross platform , Pay attention to portable programs and avoid using ) The bit segment declaration rules are as follows :
struct S {
char a:3;
char b:4;
char c:5;
char d:4;
};
struct S s = {0};
s.a = 5; s.b = 12; s.c = 3; s.d = 4;stay VS Next , The layout in memory is as follows ( The picture is a little rough ,c=3 It's on it 00011 dial the wrong number ):
It can be seen that bit segments are opened from right to left in bytes , If the size of the bit segment member is not enough, a byte will be opened up at a higher address

View the structure under the compiler s The memory content of is found to be true :
Be careful : The cross platform problem of bit segment ( For the following reasons , Bit segments do not support cross platform )
enumeration
Enumeration refers to the types that list possible values one by one , for example
Advantages of enumeration
comparison define Defined constant , Enumeration has more rigorous type checking .
Defining enumeration types and defining multiple constants at a time is fast and convenient , Enumerated constants have their own names and are of the same type , It increases the readability of the code and is easy to maintain .
Use of enumeration
enum Color// Color
{
RED=1,
GREEN=2,
BLUE=4
};
enum Color clr = GREEN;// Only enumeration constants can be assigned to enumeration variables , There will be no type difference
image clr=4; This assignment is unreasonable because enum color The type and int Different types .although enum Color and int Different types , But the compiler works properly , Finding the size of variables of enumeration type is 4 Bytes

union
Definition of joint type
Calculation of joint size
Because the members of the Federation share the same memory space , The size of such a union type variable , At least the size of the largest member , But this is not the final size of the union type variable , Because the consortium also has memory alignment
The rule is , The size of the consortium is at least the size of the largest member ; If it is not an integral multiple of the maximum alignment number of consortium members , Then align to an integer multiple .( Array alignment number is the alignment number of array member type )
for example :

First VS2022 The default alignment number of the lower compiler is 8,c The alignment number of is 1,i The alignment number of is 4, Array a The alignment number of is 1,
So united un It's at least the same size 5, Then align to 4 Integer multiple 8
Why at least 5 instead of 4 Well , Array a Is it a member or is each member in the array a member of the consortium ?
The following example illustrates :

You can see the whole array a Is a member of the consortium , Otherwise the result is 8 instead of 12 了 .
Characteristics of Union
Because the members of the consortium share a space , The characteristic of association is naturally that the memory storage is different from the structure .
for example :

i The size of is just the size of the whole consortium ,c Is part of ,
so i=0x22334455(4 byte ) when ,c Is equal to 0x55(1 byte ), It's also easy to understand from memory .
边栏推荐
- Network security B module windows operating system penetration test of national vocational college skills competition
- Jenkins detailed deployment
- 25.消息订阅与发布——PubSub-js
- Oauth2==sso three protocols. Oauth2 four modes
- Using depth and normal textures in unity
- File upload and download demo
- The goal you specified requires a project to execute but there is no POM in this directory
- Mitre att & CK ultra detailed learning notes-01 (background, terms, cases)
- Filter filter
- numpy.arange
猜你喜欢

Introduction to C language

JS_ Realize the separation of multiple lines of text into an array according to the newline

System integration project management engineer (soft test intermediate) key knowledge, recitation version

Stm32h750vbt6 drives programmable gain amplifier module pga113 -- Hal Library Based on cubemx

InjectFix原理学习(实现修复加法的热更)

Influxdb unauthorized access & CouchDB permission bypass

MITRE ATT&CK超详细学习笔记-01(背景,术语,案例)

Bookkeeping app: xiaoha bookkeeping 2 - production of registration page

DOM operation of JS -- style operation
![[line test] Figure finding regular questions](/img/61/d1c2cd399cf0d808e4fa25cd5fe681.png)
[line test] Figure finding regular questions
随机推荐
我的创作纪念日
24. Global event bus
研究会2022.07.22--对比学习
[leetcode] 11. Container with the most water - go language solution
服务漏洞&FTP&RDP&SSH&rsync
系统集成项目管理工程师(软考中级)重点知识、背诵版
【FreeRTOS】11 软件定时器
Mitre att & CK ultra detailed learning notes-01 (background, terms, cases)
23. Component customization events
Li Kou, niuke.com - > linked list related topics (Article 1) (C language)
Three implementation methods of single sign on
requests-爬取页面源码数据
CSDN, it's time to say goodbye!
Game three piece chess
Induction, generalization, deduction
[leetcode] 444. Sequence reconstruction
SPI - send 16 bit and 8-bit data
A great hymn
[information system project manager] Chapter VII recheck cost management knowledge structure
fopen、fwrite、fseek、ftell、fread使用demo