📄 struct的对齐 - 陈晓勇的专栏 - csdnblog.htm
字号:
id=_74_108_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/InBlock.gif" align=top> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000"> c;<BR><IMG
alt="" src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/InBlock.gif"
align=top> s1 d;<BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/InBlock.gif" align=top> </SPAN><SPAN
style="COLOR: #0000ff"><FONT color=#ff0000>double</FONT></SPAN><SPAN
style="COLOR: #000000"><FONT color=#ff0000> </FONT>e; // long long 类型 vc6
好像没有<BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/ExpandedBlockEnd.gif"
align=top> }</SPAN></SPAN><SPAN style="COLOR: #000000">;<BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/None.gif" align=top><BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/None.gif" align=top></SPAN><SPAN
style="COLOR: #0000ff">#pragma</SPAN><SPAN
style="COLOR: #000000"> pack()</SPAN><SPAN style="COLOR: #000000"><BR><IMG
alt="" src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/None.gif"
align=top></SPAN></DIV></DIV>
<P><BR> 问 <BR> 1.sizeof(s2) = ?<BR> 2.s2的s1中的a后面空了几个字节接着是b?</P>
<P>现在google看到的答案都是<BR><BR> 1.
24<BR> 2. 2</P>
<P>所说的理由如下:<BR> 成员对齐有一个重要的条件,即每个成员分别对齐.即每个成员按自己的方式对齐.<BR>
也就是说上面虽然指定了按8字节对齐,但并不是所有的成员都是以8字节对齐.其对齐的规则是,每个成员按其类型的对齐参数(通常是这个类型的大小)和指定对齐参数(这里是8字节)中较小的一个对齐.并且结构的长度必须为所用过的所有对齐参数的整数倍,不够就补空字节<BR><FONT
color=#0000ff> <FONT
size=3>S1中,成员a是1字节默认按1字节对齐,指定对齐参数为8,这两个值中取1,a按1字节对齐;成员b是4个字节,默认是按4字节对齐,这时就按4字节对齐,所以sizeof(S1)应该为8;<BR> S2中,c和S1中的a一样,按1字节对齐,而d
是个结构,它是8个字节,它按什么对齐呢?对于结构来说,它的默认对齐方式就是它的所有成员使用的对齐参数中最大的一个,S1的就是4.所以,成员d就是按4字节对齐.成员e是8个字节,它是默认按8字节对齐,和指定的一样,所以它对到8字节的边界上,这时,已经使用了12个字节了,所以又添加了4个字节的空,从第16个字节开始放置成员e.这时,长度为24,已经可以被8(成员e按8字节对齐)整除.这样,一共使用了24个字节.</FONT></FONT><BR><BR>-------------------------------------------------------------------------------------------------------------------------------------------------------</P>
<P>这是我测试得到的<BR><BR>答案:</P>
<P>vc 6 下<BR> 1.
24<BR> 2. 2</P>
<P>gcc version 2.95.4 20020320 [FreeBSD] / gcc version 3.4.6 [FreeBSD]
20060305 下<BR> 1.
20<BR> 2. 2</P>
<P>gcc 下struct 变成这样(红色部分),答案两者就一致了。</P>
<DIV
style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG alt="" src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/None.gif"
align=top><SPAN style="COLOR: #0000ff">#pragma</SPAN><SPAN
style="COLOR: #000000"> pack(8)</SPAN><SPAN style="COLOR: #000000"><BR><IMG
alt="" src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/None.gif" align=top><BR><IMG
id=_31_56_Open_Image
onclick="this.style.display='none'; document.getElementById('_31_56_Open_Text').style.display='none'; document.getElementById('_31_56_Closed_Image').style.display='inline'; document.getElementById('_31_56_Closed_Text').style.display='inline';"
alt="" src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/ExpandedBlockStart.gif"
align=top><IMG id=_31_56_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('_31_56_Closed_Text').style.display='none'; document.getElementById('_31_56_Open_Image').style.display='inline'; document.getElementById('_31_56_Open_Text').style.display='inline';"
alt="" src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/ContractedBlock.gif"
align=top> </SPAN><SPAN style="COLOR: #0000ff">struct</SPAN><SPAN
style="COLOR: #000000"> s1...</SPAN><SPAN id=_31_56_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</SPAN><SPAN
id=_31_56_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/InBlock.gif" align=top> </SPAN><SPAN
style="COLOR: #0000ff">short</SPAN><SPAN style="COLOR: #000000"> a;<BR><IMG
alt="" src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/InBlock.gif"
align=top> </SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN
style="COLOR: #000000"> b;<BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/ExpandedBlockEnd.gif"
align=top> }</SPAN></SPAN><SPAN style="COLOR: #000000">;<BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/None.gif" align=top><BR><IMG
id=_74_108_Open_Image
onclick="this.style.display='none'; document.getElementById('_74_108_Open_Text').style.display='none'; document.getElementById('_74_108_Closed_Image').style.display='inline'; document.getElementById('_74_108_Closed_Text').style.display='inline';"
alt="" src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/ExpandedBlockStart.gif"
align=top><IMG id=_74_108_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('_74_108_Closed_Text').style.display='none'; document.getElementById('_74_108_Open_Image').style.display='inline'; document.getElementById('_74_108_Open_Text').style.display='inline';"
alt="" src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/ContractedBlock.gif"
align=top> </SPAN><SPAN style="COLOR: #0000ff">struct</SPAN><SPAN
style="COLOR: #000000"> s2...</SPAN><SPAN id=_74_108_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</SPAN><SPAN
id=_74_108_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/InBlock.gif" align=top> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000"> c;<BR><IMG
alt="" src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/InBlock.gif"
align=top> s1 d;<BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/InBlock.gif" align=top> </SPAN><SPAN
style="COLOR: #0000ff"><FONT color=#ff0000>double</FONT></SPAN><SPAN
style="COLOR: #000000"><FONT color=#ff0000> </FONT>e;<BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/ExpandedBlockEnd.gif"
align=top> } <FONT color=#ff0000>__attribute__ ((aligned
(8)))</FONT></SPAN></SPAN><SPAN style="COLOR: #000000"><FONT
color=#ff0000>;</FONT><BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/None.gif" align=top><BR><IMG alt=""
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/None.gif" align=top></SPAN><SPAN
style="COLOR: #0000ff">#pragma</SPAN><SPAN
style="COLOR: #000000"> pack()</SPAN><SPAN style="COLOR: #000000"><BR><IMG
alt="" src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/None.gif"
align=top></SPAN></DIV></DIV>
<P>可能除了pack,aligned还是有用的。。另
pack中的数字只能为1,2,4,8,16<BR>----------------------------------------------------------------------------------------------------------------------------------------------------</P>
<P>struct
aligned 的目标是提高地址访问的效率,可能导致的问题是在数据类型强制转换时(本质还是内部成员的地址问题)出现的问题。典型的把struct
对象从原端通过网络传递到对端,再还原为struct。这个可能对端的操作平台、编译器等对struct的实际处理是不一致的。。</P>
<P><BR> </P><BR><BR>
<P id=TBPingURL>Trackback:
http://tb.blog.csdn.net/TrackBack.aspx?PostId=1560699</P><BR></DIV>
<DIV class=postFoot>
<SCRIPT src=""></SCRIPT>
[<A title=功能强大的网络收藏夹,一秒钟操作就可以轻松实现保存带来的价值、分享带来的快乐
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(saveit=window.open('http://wz.csdn.net/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=590,height=300,left=75,top=20,status=no,resizable=yes'));saveit.focus();">收藏到我的网摘</A>]
linux_chen发表于 2007年04月11日 15:57:00 </DIV></DIV><LINK
href="http://blog.csdn.net/linuxchen/Services/Pingback.aspx" rel=pingback><!--<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:dc="http://purl.org/dc/elements/1.1/"xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"><rdf:Descriptionrdf:about="http://blog.csdn.net/linuxchen/archive/2007/04/11/1560699.aspx"dc:identifier="http://blog.csdn.net/linuxchen/archive/2007/04/11/1560699.aspx"dc:title="struct的对齐"trackback:ping="http://tb.blog.csdn.net/TrackBack.aspx?PostId=1560699" /></rdf:RDF>-->
<SCRIPT>function hide(){showComment();}</SCRIPT>
<BR>
<DIV class=post>
<DIV class=postTitle>相关文章:</DIV>
<UL class=postText>
<LI><A href="http://blog.csdn.net/flyingdonkey/archive/2005/03/02/307899.aspx"
target=_blank>关于pragma的延续</A> 2005-03-02 <A
href="http://blog.csdn.net/flyingdonkey/" target=_blank>flyingdonkey</A>
<LI><A href="http://blog.csdn.net/tropic3/archive/2005/03/06/312758.aspx"
target=_blank>pack详解</A> 2005-03-06 <A href="http://blog.csdn.net/tropic3/"
target=_blank>tropic3</A>
<LI><A href="http://blog.csdn.net/quicksandj2/archive/2006/07/03/871140.aspx"
target=_blank>struct的成员对齐</A> 2006-07-03 <A
href="http://blog.csdn.net/quicksandj2/" target=_blank>quicksandj2</A>
<LI><A href="http://blog.csdn.net/bgslyao/archive/2006/04/10/658004.aspx"
target=_blank>[c-programming]编译器字节对齐问题-结构字段对齐问题</A> 2006-04-10 <A
href="http://blog.csdn.net/bgslyao/" target=_blank>bgslyao</A>
<LI><A href="http://blog.csdn.net/O_OCalmHawk/archive/2005/04/20/356126.aspx"
target=_blank>关于C编译器里字节对齐的问题</A> 2005-04-20 <A
href="http://blog.csdn.net/O_OCalmHawk/" target=_blank>O_OCalmHawk</A>
<DIV align=right><A
href="http://blog.csdn.net/linuxchen/relatedarticles/1560699.aspx"
target=_blank>更多内容...</A></DIV></LI></UL></DIV><BR><BR><BR>
<DIV class=post id=csdn_zhaig_ad_yahoo></DIV>
<SCRIPT type=text/javascript>document.write("<img src=http://counter.csdn.net/pv.aspx?id=24 border=0 width=0 height=0>");</SCRIPT>
<SPAN class=PreAndNext id=viewpost.ascx_PreviousAndNextEntriesDown>
<DIV align=center><A
href="http://blog.csdn.net/linuxchen/archive/2007/04/27/1587640.aspx">上一篇: mysql
cluster insert 测试结果</A> | <A
href="http://blog.csdn.net/linuxchen/archive/2007/04/05/1553245.aspx">下一篇: 哲学家就餐问题实现--多线程同步(unix
- windows)</A></DIV></SPAN><SPAN id=Anthem_Comments.ascx_ltlComments__><SPAN
id=Comments.ascx_ltlComments><BR>
<DIV id=comments>
<H3></H3>没有评论。 </DIV></SPAN></SPAN>
<SCRIPT language=javascript>
ad_width=468;
ad_height=60;
adcss=2;
unionuser=19;
ad_type='j';
count=5;
</SCRIPT>
<DIV>
<SCRIPT language=javascript src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/showads.js"
type=text/javascript></SCRIPT>
<SCRIPT language=javascript src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/showgm.js"
type=text/javascript></SCRIPT>
</DIV>
<DIV class=CommentForm id=commentform>
<H3>发表评论 </H3>
<DIV id=Anthem_PostComment.ascx_CommentUpdatePanel__>
<DIV id=PostComment.ascx_CommentUpdatePanel>
<TABLE class=CommentForm>
<TBODY>
<TR>
<TD width=69 height=0></TD>
<TD></TD></TR>
<TR>
<TD width=70>大名:</TD>
<TD align=left><INPUT id=PostComment.ascx_tbName style="WIDTH: 300px"
disabled maxLength=32 size=40 value=lwei_cxdn2007
name=PostComment.ascx:tbName> <SPAN
id=PostComment.ascx_RequiredFieldValidator2
style="DISPLAY: none; COLOR: red" initialvalue=""
evaluationfunction="RequiredFieldValidatorEvaluateIsValid"
display="Dynamic" errormessage="<br>请输入尊姓大名"
controltovalidate="PostComment.ascx_tbName"><BR>请输入尊姓大名</SPAN> </TD></TR>
<TR>
<TD width=70>网址:</TD>
<TD align=left><INPUT id=PostComment.ascx_tbUrl style="WIDTH: 300px"
maxLength=256 size=40 name=PostComment.ascx:tbUrl> </TD></TR>
<TR>
<TD colSpan=3>评论 <SPAN id=PostComment.ascx_RequiredFieldValidator3
style="DISPLAY: none; COLOR: red" initialvalue=""
evaluationfunction="RequiredFieldValidatorEvaluateIsValid"
display="Dynamic" errormessage="<br>请输入评论"
controltovalidate="PostComment.ascx_tbComment"><BR>请输入评论</SPAN> <BR><TEXTAREA id=PostComment.ascx_tbComment style="WIDTH: 381px; HEIGHT: 193px" name=PostComment.ascx:tbComment rows=10 cols=50></TEXTAREA>
</TD></TR>
<TR>
<TD colSpan=3><SPAN id=Anthem_PostComment.ascx_btnSubmit__><INPUT language=javascript class=Button id=PostComment.ascx_btnSubmit onclick="javascript:Anthem_FireCallBackEvent(this,event,'PostComment.ascx:btnSubmit','',true,'','','正在处理...',false,null,null,null,true,true);return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("PostComment.ascx:btnSubmit", "", true, "", "", false, false))" type=submit value=提交 name=PostComment.ascx:btnSubmit></SPAN>
</TD></TR>
<TR>
<TD colSpan=3><SPAN id=PostComment.ascx_Message
style="COLOR: red"></SPAN></TD></TR></TBODY></TABLE></DIV></DIV></DIV></DIV>
<P id=footer>Powered by: <BR><A id=Footer1_Hyperlink2
href="http://scottwater.com/blog" name=Hyperlink1><IMG
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/100x30_Logo.gif" border=0></A> <A
id=Footer1_Hyperlink3 href="http://asp.net/" name=Hyperlink1><IMG
src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/PoweredByAsp.Net.gif" border=0></A>
<BR>Copyright © linux_chen </P>
<SCRIPT src="struct的对齐 - 陈晓勇的专栏 - CSDNBlog.files/counter.js"></SCRIPT>
<SCRIPT type=text/javascript>
<!--
var Page_Validators = new Array(document.getElementById("PostComment.ascx_RequiredFieldValidator2"), document.getElementById("PostComment.ascx_RequiredFieldValidator3"));
// -->
</SCRIPT>
<INPUT id=__EVENTVALIDATION type=hidden
value=/wEWBQL+raDpAgKqtOi0AwLAsuLbDAKi687YCQKG6K6RCAHKss+LIey8yvaml2ED1YgW6ieu
name=__EVENTVALIDATION>
<SCRIPT type=text/javascript>
<!--
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
ValidatorOnLoad();
}
function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else {
return true;
}
}
// -->
</SCRIPT>
</FORM>
<SCRIPT language=javascript type=text/javascript>
<!--
try
{
hide();
}
catch(e){}
//-->
</SCRIPT>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -