当前位置:网站首页>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 .
边栏推荐
- Wild pointer, null pointer, invalid pointer
- File "manage.py", line 14) from exc ^ syntaxerror: cause and solution of invalid syntax error
- Chapter007 FPGA learning IIC bus EEPROM reading
- B. Also Try Minecraft
- Who can stand it when the project goes online
- Game three piece chess
- Li Kou, niuke.com - > linked list related topics (Article 1) (C language)
- MySQL statement
- numpy.inf
- Oauth2==sso three protocols. Oauth2 four modes
猜你喜欢

Chapter007 FPGA learning IIC bus EEPROM reading

QoS quality of service three DiffServ Model message marking and PHB

Vulnhub DC1

Buffer overflow vulnerability of network security module B in national vocational college skills competition

Problems encountered in inserting large quantities of data into the database in the project

Using depth and normal textures in unity

Paper reading: hardnet: a low memory traffic network

MITRE ATT&CK超详细学习笔记-02(大量案例)

二维平面多段线Y轴最短距离

Filter filter
随机推荐
B. Also Try Minecraft
Deep analysis of data storage in memory
Influxdb unauthorized access & CouchDB permission bypass
System integration project management engineer (soft test intermediate) key knowledge, recitation version
Oauth2==SSO三种协议。Oauth2四种模式
Harbor2.2 quick check of user role permissions
Development system selection route
Write three piece chess in C language
nacos配置中心源码分析
Buddy: core function entry
The goal you specified requires a project to execute but there is no POM in this directory
[leetcode] 444. Sequence reconstruction
requests-爬虫多页爬取肯德基餐厅位置
numpy.cumsum
csdn,是时候说再见!
php 转义字符串
Bookkeeping app: xiaoha bookkeeping 1 - production of welcome page
【信息系统项目管理师】第七章 复盘成本管理知识架构
win10声音图标有个没有声音
Using depth and normal textures in unity