当前位置:网站首页>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 .
边栏推荐
- 给一个字符串 ① 请统计出其中每一个字母出现的次数② 请打印出字母次数最多的那一对
- The goal you specified requires a project to execute but there is no POM in this directory
- Feature Selective Anchor-Free Module for Single-Shot Object Detection
- Buffer overflow vulnerability of network security module B in national vocational college skills competition
- [PTA] group programming ladder competition - Summary of exercises L3 (incomplete)
- numpy.arange
- 24. Global event bus
- QoS quality of service three DiffServ Model message marking and PHB
- Chapter007 FPGA learning IIC bus EEPROM reading
- Learning notes - distributed transaction theory
猜你喜欢

PHP link log scheme

requests-爬虫实现一个简易网页采集器

【FreeRTOS】11 软件定时器
![[steering wheel] the super favorite idea efficiency artifact save actions is uninstalled](/img/b0/54a826287154be5b758b3850e7fa51.png)
[steering wheel] the super favorite idea efficiency artifact save actions is uninstalled

requests-爬虫多页爬取肯德基餐厅位置

Reptile learning - Overview

我的创作纪念日

Li Kou, niuke.com - > linked list related topics (Article 1) (C language)

Single Gmv has increased 100 times. What is the "general rule" behind the rise of popular brands?

Deep analysis of data storage in memory
随机推荐
Injectfix principle learning (to realize the heat of repair addition)
sqli-labs简单安装
深度学习二三事-回顾那些经典卷积神经网络
Induction, generalization, deduction
Decompress the anchor and enjoy 4000w+ playback, adding a new wind to the Kwai food track?
Introduction to C language
Laplace distribution
requests-爬虫多页爬取肯德基餐厅位置
stdafx.h 简介及作用
PHP link log scheme
CSDN, it's time to say goodbye!
China trichlorosilane Market Forecast and Strategic Research Report (2022 Edition)
Pytorch deep learning practice lesson 10 / assignment (basic CNN)
Feature Selective Anchor-Free Module for Single-Shot Object Detection
Mitre att & CK ultra detailed learning notes-01 (background, terms, cases)
25.消息订阅与发布——PubSub-js
Riotboard development board series notes (IX) -- buildreoot porting matchbox
QoS quality of service 4 traffic regulation of QoS boundary behavior
[introduction to C language] zzulioj 1011-1015
23. Component customization events