打酱油的萧萧

一个打酱油的程序员
STL容器介绍
[原创]用C实现了个大小端检测代码,献丑了=,=

[转]C++中的内存对齐

萧萧 posted @ 2010年8月16日 02:39 in C/C++ with tags c++ 内存对齐 , 3324 阅读

        在我们的程序中,数据结构还有变量等等都需要占有内存,在很多系统中,它都要求内存分配的时候要对齐,这样做的好处就是可以提高访问内存的速度。

我们还是先来看一段简单的程序:

#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,如下图所示:

        希望通过此文能让你理解内存对齐的基本概念。

clicker heroes 说:
2018年3月29日 17:40

所以它占有了第二个4字节组块中的第一格,第三个也是char类型,所以它也占用一个字节

Write my university 说:
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?

run 3 说:
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

quotes4ever 说:
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/

slither io 说:
2018年10月29日 16:43

The content of the post is getting a lot of attention. Thank you for providing this information.

gstblue 说:
2018年12月11日 13:27

Such very use full information. great work. keep it up.

accurate pedia 说:
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>

slope game 说:
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.

outlook email login 说:
2019年3月18日 17:27

Thank you for sharing the post! I'm glad to find it.

Xbox gift card code 说:
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

tweakbox 说:
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

Viralvilla 说:
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>

updatepedia 说:
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>

dbc_corporation 说:
2020年12月19日 14:40

nice post thank you for sharing this post

kyrosdigital 说:
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.

dailyseo 说:
2020年12月23日 19:08

really this was great post.

NCERT Term 2 Questio 说:
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.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter