[转]C++中的内存对齐
在我们的程序中,数据结构还有变量等等都需要占有内存,在很多系统中,它都要求内存分配的时候要对齐,这样做的好处就是可以提高访问内存的速度。
我们还是先来看一段简单的程序:
#include <iostream> using namespace std; struct X1 { int i;//4个字节 char c1;//1个字节 char c2;//1个字节 }; struct X2 { char c1;//1个字节 int i;//4个字节 char c2;//1个字节 }; struct X3 { char c1;//1个字节 char c2;//1个字节 int i;//4个字节 }; int main() { cout<<"long "<<sizeof(long)<<"\n"; cout<<"float "<<sizeof(float)<<"\n"; cout<<"int "<<sizeof(int)<<"\n"; cout<<"char "<<sizeof(char)<<"\n"; X1 x1; X2 x2; X3 x3; cout<<"x1 的大小 "<<sizeof(x1)<<"\n"; cout<<"x2 的大小 "<<sizeof(x2)<<"\n"; cout<<"x3 的大小 "<<sizeof(x3)<<"\n"; return 0; }
这断程序的功能很简单,就是定义了三个结构体X1,X2,X3.这三个结构体的主要区别就是内存数据摆放的顺序,其他都一样的,另外程序输入几种基本类型所占用的字节数,以及我们这里的三个结构体所占用的字节数。
程序的结果是:
long 4 float 4 int 4 char 1 x1 的大小 8 x2 的大小 12 x3 的大小 8
结果的前面四行没有什么问题,但是我们在最后三行就可以看到三个结构占用的空间大小不一样,造成这个原因就是内部数据的摆放顺序,怎么会这样呢?
下面就是我们需要讲的内存对齐了。
内存是一个连续的块,我们可以用下面的图来表示, 它是以4个字节对一个对齐单位的:
让我们看看三个结构在内存中的布局:
首先是 X1,如下图所示:
X1 中第一个是 Int类型,它占有4字节,所以前面4格就是满了,然后第二个是char类型,这中类型只占一个字节,所以它占有了第二个4字节组块中的第一格,第三个也是char类型,所以它也占用一个字节,它就排在了第二个组块的第二格,因为它们加在一起大小也不超过一个块,所以他们三个变量在内存中的结构就是这样的,因为有内存分块对齐,所以最后出来的结果是8,而不是6,因为后面两个格子其实也算是被用了。
再次看看X2,如图所示
X2中第一个类型是Char类型,它占用一个字节,所以它首先排在第一组块的第一个格子里面,第二个是Int类型,它占用4个字节,第一组块已经用掉一格,还剩3格,肯定是无法放下第二Int类型的,因为要考虑到对齐,所以不得不把它放到第二个组块,第三个类型是Char类型,跟第一个类似。所因为有内存分块对齐,我们的内存就不是8个格子了,而是12个了。
再看看X3,如下图所示:
希望通过此文能让你理解内存对齐的基本概念。
2018年3月29日 17:40
所以它占有了第二个4字节组块中的第一格,第三个也是char类型,所以它也占用一个字节
2018年7月10日 17:55
I am completely into optimization, but now, is it truly something that can have the effect in the event that we contrast a similar program and or without its memory improved and aligned?
2018年7月11日 17:43
I am grateful to have opened this discussion. This question is quite interesting to me. Finally the answer was found
2018年9月27日 15:47
very useful information
2018年9月27日 15:47
https://quotes4ever.com/lawyers-quotes/
https://quotes4ever.com/gene-wilder-quotes/
https://quotes4ever.com/democritus-quotes/
https://quotes4ever.com/ct-fletcher-quotes/
https://quotes4ever.com/millionaire-quotes/
2018年10月08日 18:46
Best Digital Marketing Course in Surat With Job Placements Best SPC Training in Surat With Job Placements Best PPC Training in Surat With Job Placements Top Social Media Marketing Training in Surat With Job Placements
2018年10月29日 16:43
The content of the post is getting a lot of attention. Thank you for providing this information.
2018年12月11日 13:27
Such very use full information. great work. keep it up.
2019年1月12日 15:07
HumanRemainsTransfer dead-body-train-transport-services dead-body-road-transport
2019年1月28日 14:42
nice info
2019年1月28日 14:43
/><a href="https://accuratepedia.blogspot.com/2019/01/funny-wifi-names.html">
Funny Wifi Names </a><br/><br/> <a href="https://accuratepedia.blogspot.com/2019/01/accounting-software-in-ahmedabad.html">Accounting Software in Ahmedabad </a><br/><br/><a href="https://accuratepedia.blogspot.com/2019/01/logo-designer-in-surat.html">
Logo Designer in Surat </a><a href="https://accuratepedia.blogspot.com/2019/01/crm-benefits.html">CRM Benefits </a> <br /> <br /> <a href="https://accuratepedia.blogspot.com/2019/01/accounting-software-in-dubai-uae.html">Accounting Software In Dubai UAE</a> <br /> <br /> <a href="https://accuratepedia.blogspot.com/2019/01/accounting-software-in-surat.html ">Accounting Software in Surat </a> <br /> <br /> <a href="https://accuratepedia.blogspot.com/2019/01/crm-software-best-crm-software-2019.html">CRM software</a> <br /> <br /> <a href="https://accuratepedia.blogspot.com/2018/11/what-is-quickbooks-how-to-use-quickbooks-for-businesses.html">What is quickbooks</a> <br /> <br /> <a href="https://accuratepedia.blogspot.com/2018/11/how-to-transfer-balance-from-vodafone-to-vodafone.html">How to transfer balance from vodafone to vodafone</a>
2019年1月28日 14:44
Funny wifi names
Accounting Software In Dubai UAE
Accounting Software in Ahmedabad
Accounting Software in Surat
CRM Benefits
Logo Designer in Surat
CRM software
top 5 new game coming in 2019
What is quickbooks
How to transfer balance from vodafone to vodafone
2019年1月28日 14:50
Thank you so much
Success Status in English
Love Status in Hindi
Sorry Status in English
Brother Status 1000+ | Best Brother Status
Thank You Status For Whatsapp, Facebook, Instagram
Short Life Status for Whatsapp in Hindi
Golden thoughts of life in hindi
Gym Status, Workout Gym Quotes
Alone Status - Feeling Alone Status English
Cool Status in English
2019年1月31日 16:49
If there are many sad stories, share it with the people you trust. Sharing helps people get closer together and you also relieve some of that sadness.
2019年3月18日 17:27
Thank you for sharing the post! I'm glad to find it.
2019年4月10日 16:12
nice
2019年4月10日 16:13
nice
2019年4月10日 16:14
click
2019年4月23日 04:44
good work!
2019年5月22日 15:30
nice post
2019年8月11日 21:43
Enjoyed reading the article above, really explains everything in detail,the article is very interesting and effective. Thank you and good luck for the upcoming articles
2019年8月12日 12:42
I have read your post. It solved some aspects of the problem. I think you need to make the problem clearer
2019年11月16日 22:22
Thanks for sharing this post with your friends and family are doing well and that.
<a
href="https://www.viralvilla.online/2019/07/rashi-khanna-biography-wiki-age-height.html">Rashi Khanna</a>
2020年1月30日 18:51
nice post
2020年1月30日 18:52
nice post
2020年6月29日 12:34
<a href='https://www.updatepedia.com/'>updatepedia</a>
<a href='https://www.updatepedia.com/desh-bhakti-shayari/'>Desh Bhakti Shayari in Hindi Language</a>
<a href='https://www.updatepedia.com/facebook-status-in-english/'>Best Facebook Status in English </a>
2020年6月29日 12:35
nice information
2020年12月19日 14:40
nice post thank you for sharing this post
2020年12月21日 17:03
great article
2020年12月22日 16:26
nice post
2020年12月22日 19:25
At <a href="https://kyrosdigital.in">Kyros digital marketing company</a> our prime objective is to prioritize our client requirements and understand their growth curve. We make efforts to channelize every action client by offering them a competitive edge.
2020年12月23日 19:08
really this was great post.
2021年5月01日 18:48
nice post
2022年9月29日 19:58
Every 10th standard candidates who to know the examination pattern or scheme of exam can download NCERT Term-2 Sample Paper 2023 Class exams of the course. Who downloaded the sample paper suggestions can get complete stretcher of Part-A, Part-B, Part-C and Part-D exam for all formats of SA, FA and Assignments. Every 10th standard candidates who to know the examination pattern or scheme of exam can download NCERT Term-2 Sample Paper 2023 Class exams of the course. Who downloaded the sample paper suggestions can get complete stretcher of Part-A, Part-B, Part-C and Part-D exam for all formats of SA, FA and Assignments.NCERT Term 2 Question Paper Class 10 Every 10th standard candidates who to know the examination pattern or scheme of exam can download.