﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-热爱软件过程-文章分类-技术感悟</title><link>http://www.cppblog.com/ehhboy/category/224.html</link><description>对c/c++的热爱犹如对美女的热爱</description><language>zh-cn</language><lastBuildDate>Thu, 05 Jun 2008 05:08:16 GMT</lastBuildDate><pubDate>Thu, 05 Jun 2008 05:08:16 GMT</pubDate><ttl>60</ttl><item><title>如何传出函数中malloc分配的指针（转贴csdn）</title><link>http://www.cppblog.com/ehhboy/articles/993.html</link><dc:creator>逗泥丸</dc:creator><author>逗泥丸</author><pubDate>Wed, 09 Nov 2005 03:14:00 GMT</pubDate><guid>http://www.cppblog.com/ehhboy/articles/993.html</guid><wfw:comment>http://www.cppblog.com/ehhboy/comments/993.html</wfw:comment><comments>http://www.cppblog.com/ehhboy/articles/993.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ehhboy/comments/commentRss/993.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ehhboy/services/trackbacks/993.html</trackback:ping><description><![CDATA[请看先看下面，猜猜会有什么样的结果！<BR>void &nbsp;B(char*); &nbsp;<BR>void &nbsp;D(char*); &nbsp;<BR>void &nbsp;A() &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char &nbsp;* &nbsp;p &nbsp;= &nbsp;new &nbsp;char[20];//(char*)malloc(20); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;B(p); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt;"in &nbsp;A, &nbsp;address=" &nbsp;&lt;&lt;(long) &nbsp;p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt;"in &nbsp;A, &nbsp;content=" &nbsp;&lt;&lt; &nbsp;p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete(p); &nbsp;<BR>} &nbsp;<BR>void &nbsp;B(char &nbsp;* &nbsp;p) &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memcpy(p,"aaa",3); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt; &nbsp;"in &nbsp;B, &nbsp;address=" &nbsp;&lt;&lt; &nbsp;(long)p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt; &nbsp;"in &nbsp;B, &nbsp;content=" &nbsp;&lt;&lt; &nbsp;p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>} &nbsp;<BR>void &nbsp;C() &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char &nbsp;* &nbsp;p &nbsp;= &nbsp;0; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;D(p); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt;"in &nbsp;C &nbsp;after &nbsp;call &nbsp;D, &nbsp;address=" &nbsp;&lt;&lt;(long) &nbsp;p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt;"in &nbsp;C &nbsp;after &nbsp;call &nbsp;D, &nbsp;content=" &nbsp;&lt;&lt; &nbsp;p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;free(p); &nbsp;<BR>} &nbsp;<BR>void &nbsp;D(char* &nbsp;p) &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p &nbsp;= &nbsp;(char*) &nbsp;malloc(20); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memcpy(p,"aaa",3); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt; &nbsp;"in &nbsp;D, &nbsp;address=" &nbsp;&lt;&lt; &nbsp;(long)p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt; &nbsp;"in &nbsp;D, &nbsp;content=" &nbsp;&lt;&lt; &nbsp;p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>} &nbsp;<BR>&nbsp;<BR>int &nbsp;main() &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A(); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt; &nbsp;"-----" &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C(); &nbsp;<BR>} &nbsp;<BR>&nbsp;<BR>为什么A调B可以获取指针参数所指内容，而C调D无法获取内容？ &nbsp;<BR>&nbsp;<BR>--------------------------------------------------------------- &nbsp;<BR>&nbsp;<BR>void &nbsp;D(char*&amp; &nbsp;p); &nbsp;<BR>&nbsp;<BR>--------------------------------------------------------------- &nbsp;<BR>&nbsp;<BR>因为D接受的是一个“指针值参”（C不支持变参类型），所以你在D中对p的赋值不会影响C的的变量。 &nbsp;<BR>改为： &nbsp;<BR>void &nbsp;D(char** &nbsp;p) &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*p &nbsp;= &nbsp;(char*) &nbsp;malloc(20); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memcpy(*p,"aaa",3); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt; &nbsp;"in &nbsp;D, &nbsp;address=" &nbsp;&lt;&lt; &nbsp;(long)*p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt; &nbsp;"in &nbsp;D, &nbsp;content=" &nbsp;&lt;&lt; &nbsp;*p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>} &nbsp;<BR>void &nbsp;C() &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char &nbsp;* &nbsp;p &nbsp;= &nbsp;0; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;D(&amp;p); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt;"in &nbsp;C &nbsp;after &nbsp;call &nbsp;D, &nbsp;address=" &nbsp;&lt;&lt;(long) &nbsp;p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt;"in &nbsp;C &nbsp;after &nbsp;call &nbsp;D, &nbsp;content=" &nbsp;&lt;&lt; &nbsp;p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;free(p); &nbsp;<BR>} &nbsp;<BR>也就是用指针实现“变参”的方法。 &nbsp;<BR>&nbsp;<BR>--------------------------------------------------------------- &nbsp;<BR>&nbsp;<BR>因为c是传值而不是传地址的 &nbsp;<BR>void &nbsp;D(char* &nbsp;p) &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p &nbsp;= &nbsp;(char*) &nbsp;malloc(20); &nbsp;//被重新赋值但无法返回。 &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memcpy(p,"aaa",3); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt; &nbsp;"in &nbsp;D, &nbsp;address=" &nbsp;&lt;&lt; &nbsp;(long)p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt; &nbsp;"in &nbsp;D, &nbsp;content=" &nbsp;&lt;&lt; &nbsp;p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>} &nbsp;<BR>&nbsp;<BR>改为： &nbsp;<BR>char &nbsp;* &nbsp;D() &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp;p=... &nbsp;<BR>&nbsp; &nbsp; &nbsp;return &nbsp;p; &nbsp;<BR>} &nbsp;<BR>&nbsp;<BR>void &nbsp;C() &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char &nbsp;* &nbsp;p &nbsp;= &nbsp;0; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p= &nbsp;D(); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;... &nbsp;<BR>} &nbsp;<BR>&nbsp;<BR>--------------------------------------------------------------- &nbsp;<BR>&nbsp;<BR>因为c是传值而不是传地址的 &nbsp;<BR>void &nbsp;D(char* &nbsp;p) &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p &nbsp;= &nbsp;(char*) &nbsp;malloc(20); &nbsp;//被重新赋值但无法返回。 &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memcpy(p,"aaa",3); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt; &nbsp;"in &nbsp;D, &nbsp;address=" &nbsp;&lt;&lt; &nbsp;(long)p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &nbsp;&lt;&lt; &nbsp;"in &nbsp;D, &nbsp;content=" &nbsp;&lt;&lt; &nbsp;p &nbsp;&lt;&lt; &nbsp;endl; &nbsp;<BR>} &nbsp;<BR>&nbsp;<BR>改为： &nbsp;<BR>char &nbsp;* &nbsp;D() &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp;p=... &nbsp;<BR>&nbsp; &nbsp; &nbsp;return &nbsp;p; &nbsp;<BR>} &nbsp;<BR>&nbsp;<BR>void &nbsp;C() &nbsp;<BR>{ &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char &nbsp;* &nbsp;p &nbsp;= &nbsp;0; &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p= &nbsp;D(); &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;... &nbsp;<BR>} &nbsp;<BR>&nbsp;<BR>--------------------------------------------------------------- &nbsp;<BR>&nbsp;<BR>或者如snipersu所说使用引用类型（只能在C++中使用） &nbsp;<BR>--------------------------------------------------------------- &nbsp;<BR>&nbsp;<BR>用char*传递参数，实际上是传递了指针的副本。如果修改指针所指的内容，则函数调用结束后内容自然保存了变化。如果是修改了指针，则实际上是修改了指针副本本身，此时再修改其所指值，则显然不是修改的原来地址处的内容，所以函数结束后也没有看到内容变化。D中使用malloc改变了指针副本所指向的单元。 &nbsp;<BR>如果要想在被调函数中分配空间，赋值后希望调用方能获取内容，应该使用char**类型做参数。 &nbsp;<BR>--------------------------------------------------------------- &nbsp;<BR>&nbsp;<BR>因为在函数中传指针（如：p）的话，实际上是函数建立一个指针指向p所指的地方，由于才C中p是个空指针，所以虽然在D中让D的p指向了分配的空间但在C中的p还是指向空的。&nbsp;&nbsp;<BR>&nbsp;<img src ="http://www.cppblog.com/ehhboy/aggbug/993.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ehhboy/" target="_blank">逗泥丸</a> 2005-11-09 11:14 <a href="http://www.cppblog.com/ehhboy/articles/993.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C+语言struct再深层探索(原创)</title><link>http://www.cppblog.com/ehhboy/articles/895.html</link><dc:creator>逗泥丸</dc:creator><author>逗泥丸</author><pubDate>Wed, 02 Nov 2005 06:49:00 GMT</pubDate><guid>http://www.cppblog.com/ehhboy/articles/895.html</guid><wfw:comment>http://www.cppblog.com/ehhboy/comments/895.html</wfw:comment><comments>http://www.cppblog.com/ehhboy/articles/895.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ehhboy/comments/commentRss/895.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ehhboy/services/trackbacks/895.html</trackback:ping><description><![CDATA[<P style="MARGIN-BOTTOM: 12pt; TEXT-ALIGN: center" align=center><FONT face=宋体><STRONG><SPAN lang=EN-US style="FONT-SIZE: 18pt; COLOR: blue">C/C+语言struct再深层探索(原创)</SPAN></STRONG><SPAN lang=EN-US>&nbsp;&nbsp;</SPAN></FONT></P>
<P style="TEXT-JUSTIFY: inter-ideograph; MARGIN-BOTTOM: 12pt; TEXT-ALIGN: justify"><FONT face=宋体>作者<SPAN lang=EN-US>:&nbsp;&nbsp;&nbsp; ehhboy <SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>日期: 2005-11-2</SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt"><SPAN lang=EN-US>1.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US>struct</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的巨大作用</SPAN><SPAN lang=EN-US>:</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在宋保华的文章中已经说明</SPAN><SPAN lang=EN-US>.</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt"><SPAN lang=EN-US>2.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">结构体的问题</SPAN><SPAN lang=EN-US>:</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于程序员来说</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">常常遇到结构体的处理问题</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如初始化结构体变量</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对结构体变量赋值问题</SPAN><SPAN lang=EN-US>.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不知大家常常怎么进行</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我曾经任务直接</SPAN><SPAN lang=EN-US>memset(</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">结构体变量</SPAN><SPAN lang=EN-US>,…) </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">会出现不可预知的问题</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">后来觉得结构体变量空间应该是逻辑上是顺序的</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">就开始大胆使用</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">然而对</SPAN><SPAN lang=EN-US>memcpy</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">还是谨慎使用</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">更不敢使用直接赋值方式</SPAN><SPAN lang=EN-US>.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不知大家遇到这种问题怎么处理</SPAN><SPAN lang=EN-US>.</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt"><SPAN lang=EN-US>3.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">也谈结构体成员的对齐</SPAN><SPAN lang=EN-US>:</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在宋宝华的文章中提到</SPAN><SPAN lang=EN-US>:</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><B><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">自然对界</SPAN></B><B><SPAN lang=EN-US style="FONT-FAMILY: Verdana"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: Verdana">struct</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">是一种复合数据类型，其构成元素既可以是基本数据类型（如</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">int</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">、</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">long</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">、</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">float</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">等）的变量，也可以是一些复合数据类型（如</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">array</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">、</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">struct</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">、</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">union</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">等）的数据单元。对于结构体，编译器会自动进行成员变量的对齐，以提高运算效率。缺省情况下，编译器为结构体的每个成员按其自然对界（</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">natural alignment</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">）条件分配空间。各个成员按照它们被声明的顺序在内存中顺序存储，第一个成员的地址和整个结构的地址相同。</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">自然对界</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">(natural alignment)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">即默认对齐方式，是指按结构体的成员中</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">size</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">最大的成员对齐。</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><B><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">指定对界</SPAN><SPAN lang=EN-US><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">一般地，可以通过下面的方法来改变缺省的对界条件：</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana"><BR></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">　　·</SPAN><SPAN style="FONT-FAMILY: Verdana"> </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">使用伪指令</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">#pragma pack (n)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">，编译器将按照</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">n</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">个字节对齐；</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana"><BR></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">　　·</SPAN><SPAN style="FONT-FAMILY: Verdana"> </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">使用伪指令</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">#pragma pack ()</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">，取消自定义字节对齐方式。</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana"><BR></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">　　注意：如果</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">#pragma pack (n)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">中指定的</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">n</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">大于结构体中最大成员的</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">size</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">，则其不起作用，结构体仍然按照</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">size</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">最大的成员进行对界。</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana"><BR style="mso-special-character: line-break"><BR style="mso-special-character: line-break"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: Verdana"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">下面我谈谈他没有明确指出的两点</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: -21pt; mso-list: l1 level2 lfo2; tab-stops: list 42.0pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings">n<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">首先指出</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">n </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">的取值范围</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">:1,2,4,8,16;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: -21pt; mso-list: l1 level2 lfo2; tab-stops: list 42.0pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings">n<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">接着举例说明自然对齐中字符串对齐方式</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Verdana">:</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如结构体</SPAN><SPAN lang=EN-US>: struct EX</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>{</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>char a[100];</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>long<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>c;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>char<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>b;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>char * p;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>} ;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">问题发现过程</SPAN><SPAN lang=EN-US>: </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在</SPAN><SPAN lang=EN-US>32</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">位环境下</SPAN><SPAN lang=EN-US>long</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的长度是</SPAN><SPAN lang=EN-US>4</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个字节</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">字符指针长度也是</SPAN><SPAN lang=EN-US>4</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个字节所以根据自然对界其长度为</SPAN><SPAN lang=EN-US>EX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">长度是</SPAN><SPAN lang=EN-US>112;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">然而在</SPAN><SPAN lang=EN-US>64</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">位环境下</SPAN><SPAN lang=EN-US>long</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的长度是</SPAN><SPAN lang=EN-US>8</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个字节</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">字符指针长度也是</SPAN><SPAN lang=EN-US>8</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个字节</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所以应该是</SPAN><SPAN lang=EN-US>124</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个字节</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">然而运行的结果却不是</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">而是</SPAN><SPAN lang=EN-US>128.</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为了探个究竟</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">编写下列程序输出内存结果</SPAN><SPAN lang=EN-US>:</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>main()</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>{</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>char ll[2];</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>strcpy( ll, "2" );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>struct EX ex,ex2,ex3;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>memset( &amp;ex, '\0', sizeof( ex ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>memset( &amp;ex2, '\0', sizeof( ex2 ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>memset( &amp;ex3, '\0', sizeof( ex3 ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>ex.a[0] = '1';</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>ex.a[99] ='9';</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>ex.c = 0XFFFFFFFFFFFFFFFF;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>dmpvarX( &amp;ex, sizeof( ex ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>/*<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>ex2= ex;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>dmpvarX( &amp;ex2, sizeof( ex2 ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>memcpy( &amp;ex3, &amp;ex, sizeof( ex3) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>dmpvarX( &amp;ex3, sizeof( ex3 ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>*/</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>printf( " size [%d]\n", sizeof( ex.a ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>printf( " size [%d]\n", sizeof( ex.c ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>printf( " size [%d]\n", sizeof( ex.b ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>printf( " size [%d]\n", sizeof( ex.p ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>printf( " size [%d]\n", sizeof( ex ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>/* </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">用于打印内存</SPAN><SPAN lang=EN-US> */</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>int dmpvarX( void * p, int len )</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>{</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>int i;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>int blen = 8;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>char cstr[2*8+1];</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>char c;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>for( i=0; i/(2*blen)&lt;=(len-1)/(2*blen) ; i++ )</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>{</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>if( i<LEN)< SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>{</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>if( i%(2*blen)<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>== 0 )</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>memset( cstr, '\0', sizeof( cstr ) );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>c = ( (char *)p )[i];</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>printf("%02x ", (short)c );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>if ( isprint( c ) ) cstr[i%(2*blen)] = c;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>else cstr[i%(2*8)] ='.';</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>else<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>printf( "<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>" );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>if( (i+1)%blen == 0) printf(" " );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>if( (i+1)%blen == 0) printf(" " );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>if( (i+1)%(2*blen) == 0) {</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>printf("%-16s", cstr );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>printf("\n" );</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>return 0;</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在</SPAN><SPAN lang=EN-US>64</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">位环境下编译运行得到</SPAN><SPAN lang=EN-US>:</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>31 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>1...............</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>................</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>................</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>................</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>................</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>................</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>00 00 00 39 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>ff ff ff ff ff ff ff ff<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>...9............</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>00 00 00 00 00 00 00 00<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>................</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>size [100]</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>size [8]</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>size [1]</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>size [8]</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>size [128]</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">发现整数前面空出四个字节</SPAN><SPAN lang=EN-US>, </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">后来把字符串</SPAN><SPAN lang=EN-US>a</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">定义位</SPAN><SPAN lang=EN-US>99</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">发现空出来</SPAN><SPAN lang=EN-US>5</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个字节</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于</SPAN><SPAN lang=EN-US>32</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">位也空出一个自己</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这是才发现是:<BR><FONT color=#ff0000 size=6><STRONG>结构体中字符串的真正长度是自然对界的长度的整数倍</STRONG></FONT></SPAN><FONT color=#ff0000><STRONG><FONT size=6><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如过不足补齐处理</SPAN><SPAN lang=EN-US>.</SPAN></FONT></STRONG></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 42pt"><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
<P>&nbsp;</P></SPAN><img src ="http://www.cppblog.com/ehhboy/aggbug/895.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ehhboy/" target="_blank">逗泥丸</a> 2005-11-02 14:49 <a href="http://www.cppblog.com/ehhboy/articles/895.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C+语言struct深层探索(摘抄)</title><link>http://www.cppblog.com/ehhboy/articles/894.html</link><dc:creator>逗泥丸</dc:creator><author>逗泥丸</author><pubDate>Wed, 02 Nov 2005 05:29:00 GMT</pubDate><guid>http://www.cppblog.com/ehhboy/articles/894.html</guid><wfw:comment>http://www.cppblog.com/ehhboy/comments/894.html</wfw:comment><comments>http://www.cppblog.com/ehhboy/articles/894.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ehhboy/comments/commentRss/894.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ehhboy/services/trackbacks/894.html</trackback:ping><description><![CDATA[<DIV align=center>
<H1 class=aTitle><SPAN id=Title>C/C+语言struct深层探索</SPAN></H1></DIV>
<TABLE width="97%" align=center>
<TBODY>
<TR>
<TD class=gray width=168>[日期： <SPAN id=UpdateTime>2005-9-26 23:56:04</SPAN>]</TD>
<TD class=gray align=middle>来源： <A id=ComeFrom href="http://www.rayoko.com/wenzhang/go.asp?id=700" target=_blank></A><SPAN id=SourceLabel></SPAN>&nbsp; 作者： <A id=Author href="mailto:"></A><SPAN id=AuthorLabel></SPAN></TD>
<TD class=gray align=right width=160>[字体：<A href="javascript:ContentSize(18)">大</A> <A href="javascript:ContentSize(14)">中</A> <A href="javascript:ContentSize(10)">小</A>] </TD></TR></TBODY></TABLE>
<DIV class=content id=BodyLabel style="PADDING-RIGHT: 10px; DISPLAY: block; PADDING-LEFT: 10px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px">
<P><SPAN id=Content>作者：宋宝华&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;出处：PConline<BR><BR>1. struct的巨大作用<BR>　　面对一个人的大型C/C++程序时，只看其对struct的使用情况我们就可以对其编写者的编程经验进行评估。因为一个大型的C/C++程序，势必要涉及一些(甚至大量)进行数据组合的结构体，这些结构体可以将原本意义属于一个整体的数据组合在一起。从某种程度上来说，会不会用struct，怎样用struct是区别一个开发人员是否具备丰富开发经历的标志。<BR><BR>　　在网络协议、通信控制、嵌入式系统的C/C++编程中，我们经常要传送的不是简单的字节流（char型数组），而是多种数据组合起来的一个整体，其表现形式是一个结构体。<BR><BR>　　经验不足的开发人员往往将所有需要传送的内容依顺序保存在char型数组中，通过指针偏移的方法传送网络报文等信息。这样做编程复杂，易出错，而且一旦控制方式及通信协议有所变化，程序就要进行非常细致的修改。<BR><BR>　　一个有经验的开发者则灵活运用结构体，举一个例子，假设网络或控制协议中需要传送三种报文，其格式分别为packetA、packetB、packetC：<BR><BR>struct structA <BR>{<BR>int a;<BR>char b;<BR>};<BR><BR>struct structB <BR>{<BR>char a;<BR>short b;<BR>};<BR><BR>struct structC<BR>{<BR>int a;<BR>char b;<BR>float c;<BR>}<BR>　　优秀的程序设计者这样设计传送的报文：<BR><BR>struct CommuPacket<BR>{<BR>int iPacketType;　　//报文类型标志<BR>union　　　　　　//每次传送的是三种报文中的一种，使用union<BR>{<BR>&nbsp;&nbsp;struct structA packetA;<BR>&nbsp;&nbsp;struct structB packetB;<BR>&nbsp;&nbsp;struct structC packetC;<BR>}<BR>};<BR>　　在进行报文传送时，直接传送struct CommuPacket一个整体。<BR><BR>　　假设发送函数的原形如下：<BR><BR>// pSendData：发送字节流的首地址，iLen：要发送的长度<BR>Send(char * pSendData, unsigned int&nbsp;&nbsp;iLen);<BR>发送方可以直接进行如下调用发送struct CommuPacket的一个实例sendCommuPacket：<BR>Send( (char *)&amp;sendCommuPacket , sizeof(CommuPacket) );<BR>假设接收函数的原形如下：<BR>// pRecvData：发送字节流的首地址，iLen：要接收的长度<BR>//返回值：实际接收到的字节数<BR>unsigned int Recv(char * pRecvData, unsigned int&nbsp;&nbsp;iLen)；<BR>　　接收方可以直接进行如下调用将接收到的数据保存在struct CommuPacket的一个实例recvCommuPacket中：<BR><BR>Recv( (char *)&amp;recvCommuPacket , sizeof(CommuPacket) );<BR>　　接着判断报文类型进行相应处理：<BR><BR>switch(recvCommuPacket. iPacketType)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;case PACKET_A:<BR>&nbsp;&nbsp;&nbsp;&nbsp;…&nbsp;&nbsp;&nbsp;&nbsp;//A类报文处理<BR>&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;case PACKET_B:<BR>&nbsp;&nbsp;&nbsp;&nbsp;…　&nbsp;&nbsp;//B类报文处理<BR>&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;case PACKET_C:<BR>&nbsp;&nbsp;&nbsp;&nbsp;…&nbsp;&nbsp;&nbsp;//C类报文处理<BR>&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>}<BR>　　以上程序中最值得注意的是<BR><BR>Send( (char *)&amp;sendCommuPacket , sizeof(CommuPacket) );<BR>Recv( (char *)&amp;recvCommuPacket , sizeof(CommuPacket) );<BR>　　中的强制类型转换：(char *)&amp;sendCommuPacket、(char *)&amp;recvCommuPacket，先取地址，再转化为char型指针，这样就可以直接利用处理字节流的函数。<BR><BR>　　利用这种强制类型转化，我们还可以方便程序的编写，例如要对sendCommuPacket所处内存初始化为0，可以这样调用标准库函数memset()：<BR><BR>memset((char *)&amp;sendCommuPacket,0, sizeof(CommuPacket));<BR><BR>2. struct的成员对齐<BR>　　Intel、微软等公司曾经出过一道类似的面试题：<BR><BR>1. #include &lt;iostream.h&gt;<BR><BR>2. #pragma pack(8)<BR>3. struct example1<BR>4. {<BR>5. short a;<BR>6. long b;<BR>7. };<BR><BR>8. struct example2<BR>9. {<BR>10. char c;<BR>11. example1 struct1;<BR>12. short e;&nbsp;&nbsp;&nbsp;&nbsp;<BR>13. };<BR>14. #pragma pack()<BR><BR>15. int main(int argc, char* argv[])<BR>16. {<BR>17. example2 struct2;<BR><BR>18. cout &lt;&lt; sizeof(example1) &lt;&lt; endl;<BR>19. cout &lt;&lt; sizeof(example2) &lt;&lt; endl;<BR>20. cout &lt;&lt; (unsigned int)(&amp;struct2.struct1) - (unsigned int)(&amp;struct2) <BR>&lt;&lt; endl;<BR><BR>21. return 0;<BR>22. }<BR>　　问程序的输入结果是什么？<BR><BR>　　答案是：<BR><BR>8<BR>16<BR>4<BR><BR>　　不明白？还是不明白？下面一一道来：<BR><BR>2.1 自然对界<BR><BR>　　struct是一种复合数据类型，其构成元素既可以是基本数据类型（如int、long、float等）的变量，也可以是一些复合数据类型（如array、struct、union等）的数据单元。对于结构体，编译器会自动进行成员变量的对齐，以提高运算效率。缺省情况下，编译器为结构体的每个成员按其自然对界（natural alignment）条件分配空间。各个成员按照它们被声明的顺序在内存中顺序存储，第一个成员的地址和整个结构的地址相同。<BR><BR>　　自然对界(natural alignment)即默认对齐方式，是指按结构体的成员中size最大的成员对齐。<BR><BR>　　例如：<BR><BR>struct naturalalign<BR>{<BR>char a;<BR>short b;<BR>char c;<BR>};<BR>　　在上述结构体中，size最大的是short，其长度为2字节，因而结构体中的char成员a、c都以2为单位对齐，sizeof(naturalalign)的结果等于6；<BR><BR>　　如果改为：<BR><BR>struct naturalalign<BR>{<BR>char a;<BR>int b;<BR>char c;<BR>};<BR>　　其结果显然为12。<BR><BR>2.2指定对界<BR><BR>　　一般地，可以通过下面的方法来改变缺省的对界条件：<BR><BR>　　· 使用伪指令#pragma pack (n)，编译器将按照n个字节对齐；<BR>　　· 使用伪指令#pragma pack ()，取消自定义字节对齐方式。<BR><BR>　　注意：如果#pragma pack (n)中指定的n大于结构体中最大成员的size，则其不起作用，结构体仍然按照size最大的成员进行对界。<BR><BR>　　例如：<BR><BR>#pragma pack (n)<BR>struct naturalalign<BR>{<BR>char a;<BR>int b;<BR>char c;<BR>};<BR>#pragma pack ()<BR>　　当n为4、8、16时，其对齐方式均一样，sizeof(naturalalign)的结果都等于12。而当n为2时，其发挥了作用，使得sizeof(naturalalign)的结果为8。<BR><BR>　　在VC++ 6.0编译器中，我们可以指定其对界方式，其操作方式为依次选择projetct &gt; setting &gt; C/C++菜单，在struct member alignment中指定你要的对界方式。<BR><BR>　　另外，通过__attribute((aligned (n)))也可以让所作用的结构体成员对齐在n字节边界上，但是它较少被使用，因而不作详细讲解。<BR><BR>2.3 面试题的解答<BR><BR>　　至此，我们可以对Intel、微软的面试题进行全面的解答。<BR><BR>　　程序中第2行#pragma pack (8)虽然指定了对界为8，但是由于struct example1中的成员最大size为4（long变量size为4），故struct example1仍然按4字节对界，struct example1的size为8，即第18行的输出结果；<BR><BR>　　struct example2中包含了struct example1，其本身包含的简单数据成员的最大size为2（short变量e），但是因为其包含了struct example1，而struct example1中的最大成员size为4，struct example2也应以4对界，#pragma pack (8)中指定的对界对struct example2也不起作用，故19行的输出结果为16；<BR><BR>　　由于struct example2中的成员以4为单位对界，故其char变量c后应补充3个空，其后才是成员struct1的内存空间，20行的输出结果为4。<BR><BR><BR>3. C和C++间struct的深层区别<BR>　　在C++语言中struct具有了“类”　的功能，其与关键字class的区别在于struct中成员变量和函数的默认访问权限为public，而class的为private。<BR><BR>　　例如，定义struct类和class类：<BR><BR>struct structA<BR>{<BR>char a;<BR>…<BR>}<BR>class classB<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char a;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;…<BR>}<BR>　　则：<BR><BR>struct A a;<BR>a.a = 'a';&nbsp;&nbsp;&nbsp;&nbsp;//访问public成员，合法<BR>classB b;<BR>b.a = 'a';&nbsp;&nbsp;&nbsp;&nbsp;//访问private成员，不合法<BR>　　许多文献写到这里就认为已经给出了C++中struct和class的全部区别，实则不然，另外一点需要注意的是：<BR><BR>　　C++中的struct保持了对C中struct的全面兼容（这符合C++的初衷——“a better c”），因而，下面的操作是合法的：<BR><BR>//定义struct<BR>struct structA<BR>{<BR>char a;<BR>char b;<BR>int c;<BR>};<BR>structA a = {'a' , 'a' ,1};&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;定义时直接赋初值<BR>　　即struct可以在定义的时候直接以{ }对其成员变量赋初值，而class则不能，在经典书目《thinking C++ 2nd edition》中作者对此点进行了强调。<BR><BR>4. struct编程注意事项<BR>　　看看下面的程序：<BR><BR>1. #include &lt;iostream.h&gt;<BR><BR>2. struct structA<BR>3. {<BR>4. int iMember;<BR>5. char *cMember;<BR>6. };<BR><BR>7. int main(int argc, char* argv[])<BR>8. {<BR>9. structA instant1,instant2;<BR>10.char c = 'a';<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>11. instant1.iMember = 1;<BR>12. instant1.cMember = &amp;c; <BR><BR>13.instant2 = instant1;<BR><BR>14.cout &lt;&lt; *(instant1.cMember) &lt;&lt; endl;<BR><BR>15.*(instant2.cMember) = 'b';<BR><BR>16. cout &lt;&lt; *(instant1.cMember) &lt;&lt; endl;<BR><BR>17. return 0;<BR>}<BR>　　14行的输出结果是：a<BR>　　16行的输出结果是：b<BR><BR>　　Why?我们在15行对instant2的修改改变了instant1中成员的值！<BR><BR>　　原因在于13行的instant2 = instant1赋值语句采用的是变量逐个拷贝，这使得instant1和instant2中的cMember指向了同一片内存，因而对instant2的修改也是对instant1的修改。<BR><BR>　　在C语言中，当结构体中存在指针型成员时，一定要注意在采用赋值语句时是否将2个实例中的指针型成员指向了同一片内存。<BR><BR>　　在C++语言中，当结构体中存在指针型成员时，我们需要重写struct的拷贝构造函数并进行“=”操作符重载。<BR></SPAN></P>
<SCRIPT event=onload for=window type=text/javascript>ImgLoad(document.getElementById("BodyLabel"));</SCRIPT>
</DIV><img src ="http://www.cppblog.com/ehhboy/aggbug/894.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ehhboy/" target="_blank">逗泥丸</a> 2005-11-02 13:29 <a href="http://www.cppblog.com/ehhboy/articles/894.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>