当前位置:网站首页>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 !
边栏推荐
- [internship] solve the problem of too long request parameters
- How to do interface testing? After reading this article, it will be clear
- Is it safe to buy funds on securities accounts? Where can I buy funds
- Spark source code compilation, cluster deployment and SBT development environment integration in idea
- 笔记本安装TIA博途V17后出现蓝屏的解决办法
- Taiwan SSS Xinchuang sss1700 replaces cmmedia cm6533 24bit 96KHz USB audio codec chip
- The metamask method is used to obtain account information
- Sometimes only one line of statements are queried, and the execution is slow
- In depth understanding of modern web browsers (I)
- 面试经验总结,为你的offer保驾护航,满满的知识点
猜你喜欢
Friends who firmly believe that human memory is stored in macromolecular substances, please take a look
Self-Improvement! Daliangshan boys all award Zhibo! Thank you for your paper
Detailed upgrade process of AWS eks
台湾SSS鑫创SSS1700替代Cmedia CM6533 24bit 96KHZ USB音频编解码芯片
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
How to do interface testing? After reading this article, it will be clear
Jetson XAVIER NX上ResUnet-TensorRT8.2速度與顯存記錄錶(後續不斷補充)
Activation function - relu vs sigmoid
In depth understanding of modern web browsers (I)
【Hot100】21. Merge two ordered linked lists
随机推荐
for(auto a : b)和for(auto &a : b)用法
[real case] trap of program design - beware of large data
Attack and defense world PWN question: Echo
burp 安装 license key not recognized
[NLP] a detailed generative text Abstract classic paper pointer generator
Research and Analysis on the current situation of China's clamping device market and forecast report on its development prospect
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of power management units in the global market in 2022
Properties of expectation and variance
Is it safe to buy funds on securities accounts? Where can I buy funds
API文档工具knife4j使用详解
Burp install license key not recognized
upload-labs
Development skills of rxjs observable custom operator
「 工业缺陷检测深度学习方法」最新2022研究综述
Google Earth Engine(GEE)——Landsat 9影像全波段影像下载(北京市为例)
【Hot100】21. 合并两个有序链表
Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
Resunnet - tensorrt8.2 Speed and Display record Sheet on Jetson Xavier NX (continuously supplemented)
Detailed upgrade process of AWS eks
Automated video production