当前位置:网站首页>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 .
边栏推荐
- Installation of pytorch in pycharm
- How do websites use CDN? What are the benefits of using it?
- Life reopens simulation / synthetic big watermelon / small air conditioner Inventory of 2021 popular open source projects
- TCP three handshakes and four waves
- How to install CentOS 6.5 PHP extension
- web渗透测试----5、暴力破解漏洞--(1)SSH密码破解
- 13. Tencent cloud IOT device side learning - data template function and Implementation
- 3. go deep into tidb: perform optimization explanation
- 【代码随想录-动态规划】T392.判断子序列
- well! Do you want to have a romantic date with the shining "China Star"?
猜你喜欢

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

SQL注入绕过安全狗思路一

在pycharm中pytorch的安装

Black hat SEO practice: General 301 weight PR hijacking

halcon知识:区域(Region)上的轮廓算子(2)

Black hat actual combat SEO: never be found hijacking

老弹出explorer.exe遇到问题已停止工作,怎么办?

An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!

讲讲我的不丰富的远程办公经验和推荐一些办公利器 | 社区征文

flutter系列之:flutter中的offstage
随机推荐
内存泄漏之KOOM
web渗透测试----5、暴力破解漏洞--(4)Telnet密码破解
[numpy] numpy's judgment on Nan value
Psexec right raising
讲讲我的不丰富的远程办公经验和推荐一些办公利器 | 社区征文
左滑从小窗到大窗口DispatchFrameLayout
Black hat SEO actual combat search engine snapshot hijacking
Create a telepresence USB drive using the DD command
Idea 1 of SQL injection bypassing the security dog
High quality travel on national day, visual start of smart Tourism
How EDI changes supply chain management
Record the creation process of a joke widget (II)
Using RDM (Remote Desktop Manager) to import CSV batch remote
Use the fluxbox desktop as your window manager
2021-10-02: word search. Given an M x n two-dimensional character grid boa
golang clean a slice
Web penetration test - 5. Brute force cracking vulnerability - (7) MySQL password cracking
Independent innovation and localization technology: SMT production line monitoring and management visualization of intelligent manufacturing
Protect your system with fail2ban and firewalld blacklists
Record a programming contest