当前位置:网站首页>Roommate, a king of time, I took care of the C language structure memory alignment
Roommate, a king of time, I took care of the C language structure memory alignment
2022-07-02 20:33:00 【Hai good】


C Memory alignment rules for language structures :
1. The first member is offset from the structure variable by 0 The address of .
2. Other member variables are aligned to a number ( Align numbers ) An integral multiple of the address of .
3. The total size of the structure is the maximum number of alignments ( Each member variable has an alignment number ) Integer multiple .
4. If the structure is nested , The nested structure is aligned to an integral multiple of its maximum alignment , The overall size of the structure is the maximum number of alignments ( The number of alignments with nested structures ) Integer multiple .
attach 1:
Align numbers = Compiler default alignment number And The smaller value of the member size .
VS The default value in is 8
Next, the landlord uses some examples to help you understand the structural alignment rules ( notes : The building owner uses VS2013 compiler )
attach 2: Common types are VS The amount of space occupied in the compiler

C Language standards :
sizeof(long long)>=sizeof(long)>=sizeof(int)
Example 1:

1、 Because the first type is int type ( Occupy 4 Bytes ), The compiler will default to an offset of 0 The address of .
2、 From the second structure member and other subsequent member variables to be aligned to a number ( Align numbers ) An integral multiple of the address of .double Type account 8 Bytes ,VS2013 The default number of alignments is 8, The smaller of the two is 8, So the alignment number is 8, The address to align to is 8 Integer multiple , So when you store it down , It can only be put in 8 The address of , skip 4、5、6、7 address , Because they're not 8 Integer multiple , Cannot be used to store , So we can only waste this space , Because the structure should be aligned .
3、 The third structure member variable is char type , The space occupied is 1, Follow VS The default number of alignments 8 Compare , smaller , Therefore, the default alignment number of the third structure member is 1, And the next address is 16,16 yes 1 Integer multiple , therefore char The type is put directly into 16 Location at address 
4、 The space occupied by such a structure is 17 Bytes , But you think the memory space of this structure takes up 17 A byte ? No , There are also three rules for structure alignment : The total size of the structure is the maximum number of alignments ( Each member variable has an alignment number ) Integer multiple . The maximum number of alignments in this structure is double The corresponding 8, So , The final memory space occupied by the structure must be 8 Integer multiple , It will automatically complete .
5、 The end result is that this structure accounts for 24 Bytes 
Example 2 :

1、 According to the first rule of structure memory alignment , Will now char The type is offset by 0 The address of .

2、 According to our second rule and 4 A rule : If the structure is nested , The nested structure is aligned to an integral multiple of its maximum alignment , The overall size of the structure is the maximum number of alignments ( The number of alignments with nested structures ) Integer multiple . The maximum number of alignments inside the structure is double Type corresponding to 8, So the structure should be from the address to 8 Start storing at an integer multiple of .
3、int The space occupied by type is 4,VS The compiler default alignment number is 8, The smaller value is 4, So the alignment number is 4,32 yes 4 Integer multiple , So from 32 Start storing... At the address int type .
4、 The size of the structure calculated at this time is 36, But the maximum number of alignments is student Inside the structure double Type corresponding to 8, The size of the structure must be an integral multiple of the maximum number of alignments , So we need to make up 
5、 So the final result of the calculated structure size is 40!
attach 2:
But everything I can't explain , I can say C The language standard prescribes , That's it , No why? ,C The structure memory alignment rule in language is like this , No why? , Everything is regulated by the standard !
remarks :
The landlord is not talented , No joy, no spray. , If there are mistakes or areas that need to be improved , Thank you very much for pointing out , I will actively learn to adopt . Thank you for your support and encouragement , I will continue to work hard and create more high-quality articles to report back and forth to my family . Programming hobby xdm, If you have any questions about programming learning, you can discuss them with me ( I try my best to help ), After all “ People gather firewood and the fire is high ”, Let's exchange and study together , Common progress !


边栏推荐
- AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
- 想请教一下,究竟有哪些劵商推荐?手机开户是安全么?
- 【JS】获取hash模式下URL的搜索参数
- 【Hot100】23. Merge K ascending linked lists
- ROS learning (10): ROS records multiple topic scripts
- 有时候只查询一行语句,执行也慢
- CRM客户关系管理系统
- Research Report on the overall scale, major manufacturers, major regions, products and applications of metal oxide arresters in the global market in 2022
- Burp install license key not recognized
- 【Hot100】22. 括号生成
猜你喜欢

One side is volume, the other side is layoff. There are a lot of layoffs in byte commercialization department. What do you think of this wave?

Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..

数据库模式笔记 --- 如何在开发中选择合适的数据库+关系型数据库是谁发明的?

Taiwan SSS Xinchuang sss1700 replaces cmmedia cm6533 24bit 96KHz USB audio codec chip

Properties of expectation and variance

Sometimes only one line of statements are queried, and the execution is slow

笔记本安装TIA博途V17后出现蓝屏的解决办法

Redis sentinel cluster working principle and architecture deployment # yyds dry goods inventory #

勵志!大凉山小夥全獎直博!論文致謝看哭網友

Basic concept of database, installation and configuration of database, basic use of MySQL, operation of database in the project
随机推荐
GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
B端电商-订单逆向流程
[JS] get the search parameters of URL in hash mode
疫情封控65天,我的居家办公心得分享 | 社区征文
什么叫在线开户?现在网上开户安全么?
Postman接口测试实战,这5个问题你一定要知道
Wu Enda's machine learning mind mapping insists on clocking in for 23 days - building a knowledge context, reviewing, summarizing and replying
Is it safe to buy funds on securities accounts? Where can I buy funds
Research Report on the overall scale, major manufacturers, major regions, products and applications of battery control units in the global market in 2022
励志!大凉山小伙全奖直博!论文致谢看哭网友
Properties of expectation and variance
想问问,现在开户有优惠吗?在线开户是安全么?
Share several map bed websites for everyone to share pictures
pytorch 模型保存的完整例子+pytorch 模型保存只保存可訓練參數嗎?是(+解决方案)
Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
In depth understanding of modern web browsers (I)
想请教一下,究竟有哪些劵商推荐?手机开户是安全么?
Database schema notes - how to choose the right database in development + who invented relational database?
AMD's largest transaction ever, the successful acquisition of Xilinx with us $35billion
Overview of browser caching mechanism