当前位置:网站首页>Structure size calculation of C language struct
Structure size calculation of C language struct
2022-06-24 03:53:00 【Good pie notes】
Go straight to the topic , To judge the space occupied by a structure , Generally speaking, it is divided into three steps :
1. First determine the actual alignment unit , It is determined by three factors
1> CPU cycle
WIN vs qt Default 8 Byte alignment
Linux 32 position Default 4 Byte alignment ,64 Bit default 8 Byte alignment
2> The largest member of the structure ( Basic data type variable )
3> Precompiling instructions #pragma pack(n) Manual settings n-- It can only be filled in 1 2 4 8 16
The smallest of the above three , Is the actual alignment unit ( there “ Actual alignment units ” It is for the convenience of me to distinguish the concept of casually taking )
2. Except for the first member of the structure , The address of all other members is relative to the structure address ( The address of its first member ) The offset of must be Actual alignment units Or an integral multiple of its own size ( Take the smaller of the two )
3. The overall size of the structure must be Actual alignment units Integer multiple .
The above three steps are the universal formula , Let's look at a practical example (linux 64 Under the system ):
1.
nums in , The alignment unit is not set manually ,linux64 The default alignment unit of the system is 8 byte , Structure nums Maximum member of double d Occupy 8 Bytes , Therefore, the actual alignment byte is the smallest of the two , namely 8 byte .
char a At the starting address of the structure ;
short b Occupy 2 Bytes ,2 Less than the actual aligned bytes 8, so b The starting address of is relative to a The offset of the start address of must be 2 An integer multiple of bytes ;
int c Occupy 4 Bytes ,4 Less than the actual aligned bytes 8, so c The starting address is relative to a The offset of the start address of must be 4 An integer multiple of bytes ;
double d Occupy 8 Bytes ,8 Align bytes with actual 8 equal , so d The starting address of is relative to a The offset of the start address of must be 8 An integer multiple of bytes ;
therefore nums The space occupied is as follows : 1(a)+1( Wasted space , from b The starting address of determines this 1 Bytes must be vacated )+2(b)+4(c)+8(d)=16 Bytes
2. Add a... At the end of the structure char Array , Let's see :
To member double d until , Structure nums The space occupied is 16, It has been analyzed above , Then there is a char Type of the array , The type of array is char[13], It's not a basic data type , It is still regarded as 13 individual char Type variable to handle ,char Occupy 1 Bytes , Less than the actual aligned bytes 8, So this 13 individual char Type variables can be directly next to double d Back placed ( The final result looks like the entire array is next to double d place ); So the general space situation is :1(a)+1( Waste space )+2(b)+4(c)+8(d)+13(arr)=29; but 29 Not satisfied with the last step of the above three steps :“ The size of the entire structure must be an integral multiple of the actual alignment unit ”, therefore 29+5( Waste space )=32, So finally nums The spatial situation of is 1(a)+1( Waste space )+2(b)+4(c)+8(d)+13(arr)+5( Waste space )=32 byte
In addition, the byte alignment of nested structures is the same as the above principle , The only thing to note is that the distance between the starting address of the substructure and the starting address of the parent structure must be the largest member of the substructure or the actual alignment unit ( Or take the smaller one ) Integer multiple .
边栏推荐
- How to bypass CDN to get web pages? How many options are available?
- High quality travel on national day, visual start of smart Tourism
- In the post epidemic era, "cloud live broadcast" saves "cloud cultural tourism"?
- Yuanqi forest pushes "youkuang", and farmers' mountain springs follow the "roll"?
- Life reopens simulation / synthetic big watermelon / small air conditioner Inventory of 2021 popular open source projects
- NLP task summary introduction and understanding
- On game safety (I)
- 黑帽SEO实战之通用301权重pr劫持
- Web penetration test - 5. Brute force cracking vulnerability - (1) SSH password cracking
- RPM 包的构建 - SPEC 基础知识
猜你喜欢

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!

浅谈游戏安全 (一)

黑帽SEO实战搜索引擎快照劫持

在pycharm中pytorch的安装

【代码随想录-动态规划】T392.判断子序列

黑帽SEO实战之目录轮链批量生成百万页面

Modstartcms enterprise content site building system (supporting laravel9) v4.2.0

Modstartcms theme introductory development tutorial

Black hat SEO actual combat search engine snapshot hijacking

Ar 3D map technology
随机推荐
Modstartcms theme introductory development tutorial
Summary of common problems of real-time audio and video TRTC - quality
黑帽SEO实战之通用301权重pr劫持
web渗透测试----5、暴力破解漏洞--(7)MYSQL密码破解
Received status code 502 from server: Bad Gateway
Actual battle case | refuse information disclosure, Tencent cloud helps e-commerce fight against web crawlers
ClickHouse(02)ClickHouse架构设计介绍概述与ClickHouse数据分片设计
TCP three handshakes and four waves
Iguard6.0 - appropriate website protection system
Web penetration test - 5. Brute force cracking vulnerability - (7) MySQL password cracking
Life reopens simulation / synthetic big watermelon / small air conditioner Inventory of 2021 popular open source projects
golang clean a slice
Black hat actual combat SEO: never be found hijacking
Prometheus PushGateway 碎碎念
Web penetration test - 5. Brute force cracking vulnerability - (1) SSH password cracking
Old popup explorer Exe has stopped working due to problems. What should I do?
Dialogue with Google technical experts: soundstream is expected to be used for general audio coding in the future
Discussion on the introduction principle and practice of fuzzy testing
Prometheus pushgateway
Exploration of web application component automatic discovery