📄 c struct 中字节对齐问题 - ehui928的专栏 - csdnblog.htm
字号:
<LI class=listitem><A href="http://www.itpub.net/forum61.html"
target=_blank>itpub</A>
<LI class=listitem><A href="http://www.codepub.com/" target=_blank>源码网</A>
<LI class=listitem><A href="http://www.infoxa.com/"
target=_blank>西安信息资源网</A></LI></UL>
<H3 class=listtitle>favor</H3>
<UL class=list>
<LI class=listitem><A href="http://www.dansaks.com/articles.htm"
target=_blank>dansaks专栏2(old)</A>
<LI class=listitem><A href="http://www.embedded.com/columns/pp/"
target=_blank>Embedded C Expert Dan Saks's Column1</A>
<LI class=listitem><A href="http://chinese.joelonsoftware.com/"
target=_blank>Joel on Software 周思博趣谈软件</A></LI></UL>
<H3 class=listtitle>jobs</H3>
<UL class=list>
<LI class=listitem><A href="http://www.gowrikumar.com/" target=_blank>C
puzzles & interview questions</A>
<LI class=listitem><A href="http://www.bewww.net/" target=_blank>IT公司速查手册</A>
<LI class=listitem><A href="http://www.chinahr.com/" target=_blank>中华英才网</A>
<LI class=listitem><A href="http://www.51job.com/" target=_blank>前程无优</A>
<LI class=listitem><A href="http://www.yingjiesheng.com/"
target=_blank>应届生求职网</A>
<LI class=listitem><A href="http://jx.edu.sina.com.cn/jdst/"
target=_blank>新浪网名企面试笔题集</A>
<LI class=listitem><A href="http://www.zhaopin.com/"
target=_blank>智联招聘网</A></LI></UL>
<H3 class=listtitle>Math</H3>
<UL class=list>
<LI class=listitem><A href="http://mathpages.com/home/index.htm"
target=_blank>MathPages</A>
<LI class=listitem><A href="http://mathworld.wolfram.com/"
target=_blank>MathWorld</A></LI></UL>
<H3 class=listtitle>Programming&Development</H3>
<UL class=list>
<LI class=listitem><A href="http://www.cpper.com/" target=_blank>All About
Program</A>
<LI class=listitem><A href="http://www.artima.com/" target=_blank>artima
developer</A>
<LI class=listitem><A href="http://www.cs.princeton.edu/~bwk/"
target=_blank>Brian Kerniphan's homepage at prinston university</A>
<LI class=listitem><A href="http://www.cppreference.com/" target=_blank>C/C++
Reference </A>
<LI class=listitem><A href="http://www.cuj.com/" target=_blank>C/C++ Users
Journal</A>
<LI class=listitem><A href="http://www.codeguru.com/"
target=_blank>CodeGuru</A>
<LI class=listitem><A href="http://www.codeproject.com/"
target=_blank>codeproject</A>
<LI class=listitem><A href="http://theory.stanford.edu/~amitp/GameProgramming"
target=_blank>GameProgramming</A>
<LI class=listitem><A href="http://www.cogitollc.com/" target=_self>M. Tim
Jones的主页--AI, LINUX编程</A>
<LI class=listitem><A href="http://www.programmersheaven.com/"
target=_blank>programmersheaven.com</A></LI></UL>
<H3 class=listtitle>技术站点、BBS、BLOG等</H3>
<UL class=list>
<LI class=listitem><A href="http://www.chinaunix.net/" target=_blank>CU</A>
<LI class=listitem><A href="http://www.mscenter.edu.cn/blog/k_eckel/"
target=_blank>k_eckel's 微软高校blog </A>
<LI class=listitem><A href="http://www.paulgraham.com/articles.html"
target=_blank>一系列关于软件开发技术和商业的文档,很值得一看</A>
<LI class=listitem><A href="http://www.cnpaf.net/" target=_blank>中国协议网</A>
<LI class=listitem><A href="http://www.embeded.cn/"
target=_blank>国内嵌入式开发网站,有很多技术资料</A></LI></UL>
<H3 class=listtitle>算法</H3>
<UL class=list>
<LI class=listitem><A href="http://www.cs.sunysb.edu/~skiena/"
target=_blank>Steven Skiena主页(主要关于算法的一些资料)</A>
<LI class=listitem><A href="http://www2.toki.or.id/book/AlgDesignManual/"
target=_blank>The Algorithm Design Manual一书的网页</A>
<LI class=listitem><A href="http://www.sparknotes.com/cs/"
target=_blank>计算机课程的notes,guides(也有math,physics, etc),很不错!</A></LI></UL>
<H3 class=listtitle>网上书店</H3>
<UL class=list>
<LI class=listitem><A href="http://www.china-pub.com/"
target=_blank>第一网上书店</A>
<LI class=listitem><A href="http://www.dearbook.com.cn/"
target=_blank>第二网上书店</A></LI></UL>
<H3 class=listtitle>存档</H3>
<UL class=list>
<LI><A
href="http://blog.csdn.net/ehui928/archive/2007/01.aspx">2007年01月(3)</A>
<LI><A
href="http://blog.csdn.net/ehui928/archive/2006/09.aspx">2006年09月(2)</A>
<LI><A
href="http://blog.csdn.net/ehui928/archive/2006/08.aspx">2006年08月(22)</A>
<LI><A
href="http://blog.csdn.net/ehui928/archive/2006/07.aspx">2006年07月(4)</A>
<LI><A
href="http://blog.csdn.net/ehui928/archive/2006/05.aspx">2006年05月(2)</A>
<LI><A
href="http://blog.csdn.net/ehui928/archive/2006/04.aspx">2006年04月(7)</A>
<LI><A
href="http://blog.csdn.net/ehui928/archive/2006/03.aspx">2006年03月(1)</A>
<LI><A
href="http://blog.csdn.net/ehui928/archive/2005/12.aspx">2005年12月(9)</A></LI></UL><SPAN
id=Anthem_RecentComments1_ltlComments__><SPAN id=RecentComments1_ltlComments>
<H3 class=listtitle>最近评论</H3>
<UL class=list>
<LI class=listitem>zxfnc:<A title=点击查看《回复:基本蚁群算法解决TSP问题的C语言实现》
href="http://blog.csdn.net/ehui928/archive/2007/01/28/1496226.aspx#573357">最的也在看这方面的资料,可以参看一下您程序吗?<BR>zxfnc@hotmail.com<BR>谢谢了!!</A>
<LI class=listitem>xsq2007:<A title=点击查看《回复:基本蚁群算法解决TSP问题的C语言实现》
href="http://blog.csdn.net/ehui928/archive/2007/01/28/1496226.aspx#564353">可以把蚁群求TSP问题C程序代码给我发一份吗?<BR>谢谢了
wzjxiaoyao123@sina.com</A>
<LI class=listitem>hyz_zsu:<A title=点击查看《回复:基本蚁群算法解决TSP问题的C语言实现》
href="http://blog.csdn.net/ehui928/archive/2007/01/28/1496226.aspx#559662">http://blog.csdn.net/ehui928/archive/2007/01/28/1496226.aspx#559662</A>
<LI class=listitem>ytsnowy:<A title=点击查看《回复:基本蚁群算法解决TSP问题的C语言实现》
href="http://blog.csdn.net/ehui928/archive/2007/01/28/1496226.aspx#555406">http://blog.csdn.net/ehui928/archive/2007/01/28/1496226.aspx#555406</A>
<LI class=listitem>ROCK001:<A title=点击查看《回复:基本蚁群算法解决TSP问题的C语言实现》
href="http://blog.csdn.net/ehui928/archive/2007/01/28/1496226.aspx#545256">我的邮箱181700765@qq.com
能给我发分吗?<BR>我的QQ181700765 能加下我吗?
我对这个问题很感兴趣。<BR></A></LI></UL></SPAN></SPAN></DIV>
<DIV id=main>
<DIV class=Tag>
<SCRIPT language=javascript
src="C struct 中字节对齐问题 - ehui928的专栏 - CSDNBlog.files/urltag.aspx"></SCRIPT>
<DIV style="CLEAR: both"></DIV></DIV><SPAN class=PreAndNext
id=viewpost.ascx_PreviousAndNextEntriesUp>
<DIV align=center><A
href="http://blog.csdn.net/ehui928/archive/2005/12/10/548671.aspx">上一篇: linux下代码阅读环境的快速建立--lxr+glimpse</A> | <A
href="http://blog.csdn.net/ehui928/archive/2005/12/07/546390.aspx">下一篇: C/C++语言void及void指针深层探索(转载)</A></DIV></SPAN><BR>
<SCRIPT>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</SCRIPT>
<DIV class=post>
<DIV class=postTitle><A
href="http://blog.csdn.net/ehui928/archive/2005/12/07/546391.aspx"><IMG
height=13 src="C struct 中字节对齐问题 - ehui928的专栏 - CSDNBlog.files/zhuan.gif"
width=15 border=0> C struct 中字节对齐问题</A> </DIV>
<DIV class=postText>
<P>VC中下面几个结构体大小分别是多少呢<BR>struct MyStruct <BR>{ <BR>
double m4; <BR> char m1; <BR> int
m3; <BR>}; </P>
<P>struct MyStruct { <BR> char m1;
<BR> double m4; <BR> int m3; <BR>};
</P>
<P>#pragma pack(push)//保存对齐状态 <BR>#pragma pack(16)
//设置为16字节对齐<BR>struct test <BR>{ <BR> char m1;
<BR> int m3; <BR> double
m4; <BR>}; <BR>#pragma pack(pop)//恢复对齐状态
<BR>如果你的答案不是16,24和16,相信下面的内容对你很有帮助。</P>
<P>1、 sizeof应用在结构上的情况 <BR>请看下面的结构: <BR>struct MyStruct <BR>{
<BR>double dda1; <BR>char dda; <BR>int type <BR>};
<BR>对结构MyStruct采用sizeof会出现什么结果呢?sizeof(MyStruct)为多少呢?也许你</P>
<P>会这样求: <BR>sizeof(MyStruct)=sizeof(double)+sizeof(char)+sizeof(int)=13
<BR>但是当在VC中测试上面结构的大小时,你会发现sizeof(MyStruct)为16。你知道为什</P>
<P>么在VC中会得出这样一个结果吗? <BR>其实,这是VC对变量存储的一个特殊处理。为了提高CPU的存储速度,VC对一些变量的</P>
<P>起始地址做了“对齐”处理。在默认情况下,VC规定各成员变量存放的起始地址相对于结</P>
<P>构的起始地址的偏移量必须为该变量的类型所占用的字节数的倍数。下面列出常用类型的</P>
<P>对齐方式(vc6.0,32位系统)。<BR>类型
对齐方式(变量存放的起始地址相对于结构的起始地址的偏移量) <BR>Char
偏移量必须为sizeof(char)即1的倍数 <BR>Short
偏移量必须为sizeof(short)即2的倍数 <BR>int 偏移量必须为sizeof(int)即4的倍数
<BR>float 偏移量必须为sizeof(float)即4的倍数 <BR>double 偏移量必须为sizeof(double)即8的倍数
<BR>各成员变量在存放的时候根据在结构中出现的顺序依次申请空间,同时按照上面的对</P>
<P>齐方式调整位置,空缺的字节VC会自动填充。同时VC为了确保结构的大小为结构的字节边</P>
<P>界数(即该结构中占用最大空间的类型所占用的字节数)的倍数,所以在为最后一个成员</P>
<P>变量申请空间后,还会根据需要自动填充空缺的字节。 <BR>下面用前面的例子来说明VC到底怎么样来存放结构的。 <BR>struct
MyStruct <BR>{ <BR>double dda1; <BR>char dda; <BR>int type <BR>};
<BR>为上面的结构分配空间的时候,VC根据成员变量出现的顺序和对齐方式,先为第一个</P>
<P>成员dda1分配空间,其起始地址跟结构的起始地址相同(刚好偏移量0刚好为sizeof(doub</P>
<P>le)的倍数),该成员变量占用sizeof(double)=8个字节;接下来为第二个成员dda分配空</P>
<P>间,这时下一个可以分配的地址对于结构的起始地址的偏移量为8,是sizeof(char)的倍数</P>
<P>,所以把dda存放在偏移量为8的地方满足对齐方式,该成员变量占用 sizeof(char)=1个字</P>
<P>节;接下来为第三个成员type分配空间,这时下一个可以分配的地址对于结构的起始地址</P>
<P>的偏移量为9,不是sizeof (int)=4的倍数,为了满足对齐方式对偏移量的约束问题,VC自</P>
<P>动填充3个字节(这三个字节没有放什么东西),这时下一个可以分配的地址对于结构的起</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -