﻿<?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++博客-梦中乐园-文章分类-C/C++</title><link>http://www.cppblog.com/Dream5/category/16724.html</link><description>C/C++程序员之梦</description><language>zh-cn</language><lastBuildDate>Fri, 16 Dec 2011 12:25:14 GMT</lastBuildDate><pubDate>Fri, 16 Dec 2011 12:25:14 GMT</pubDate><ttl>60</ttl><item><title>匈牙利表示法</title><link>http://www.cppblog.com/Dream5/articles/162161.html</link><dc:creator>梦五</dc:creator><author>梦五</author><pubDate>Thu, 15 Dec 2011 03:05:00 GMT</pubDate><guid>http://www.cppblog.com/Dream5/articles/162161.html</guid><wfw:comment>http://www.cppblog.com/Dream5/comments/162161.html</wfw:comment><comments>http://www.cppblog.com/Dream5/articles/162161.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Dream5/comments/commentRss/162161.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Dream5/services/trackbacks/162161.html</trackback:ping><description><![CDATA[许多Windows程序员使用一种叫做&#8220;匈牙利表示法&#8221;的变量名约定。这是为了纪念传奇性的 Microsoft 程序员 Charles Simonyi。<br /><br /><strong>匈牙利表示法非常简单，变量名以一个或者多个小写字母开始，这些字母表示变量的数据类型。<br />在命名结构变量时，可以用结构名（或者结构名的一种缩写）的小写作为变量名的前缀，或者用作整个变量名。<br /><br /></strong>匈牙利表示法能够帮助程序员及早发现并避免代码中的错误。由于变量名既描述了变量的作用，又描述了其数据类型，这就比较容易避免产生数据类型不匹配的错误。<br /><br />前缀&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 数据类型
<hr />
c&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char 或 WCHAR 或 TCHAR<br />by&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BYTE（无符号字符）<br />n&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; short<br />i&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int<br />x,y&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 分别用作x坐标和y坐标<br />cx,cy&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 分别用作x长度和y长度；C代表"count"<br />b 或 f&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL（int）;f代表&#8220;flag&#8221;<br />w&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WORD（无符号short）<br />l&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LONG（长整数）<br />dw&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD（无符号长整数）<br />fn&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function（函数）<br />s&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string（串）<br />sz&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;以0字节结尾的串<br />h&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 句柄<br />p&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;指针<br />
<hr />
<br /><br /><br /><img src ="http://www.cppblog.com/Dream5/aggbug/162161.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Dream5/" target="_blank">梦五</a> 2011-12-15 11:05 <a href="http://www.cppblog.com/Dream5/articles/162161.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>malloc/free  And  new/delete</title><link>http://www.cppblog.com/Dream5/articles/161772.html</link><dc:creator>梦五</dc:creator><author>梦五</author><pubDate>Thu, 08 Dec 2011 08:18:00 GMT</pubDate><guid>http://www.cppblog.com/Dream5/articles/161772.html</guid><wfw:comment>http://www.cppblog.com/Dream5/comments/161772.html</wfw:comment><comments>http://www.cppblog.com/Dream5/articles/161772.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Dream5/comments/commentRss/161772.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Dream5/services/trackbacks/161772.html</trackback:ping><description><![CDATA[<div>1. malloc/free是C++/C语言的标准库函数，new/delete是C++的运算符。它们都可用于申请动态内存和释放内存。<br /><br />2. 对于非内部数据类型的对象而言，光用malloc/free无法满足动态对象的要求。对象在创建的时候要自动执行构造函数，对象在<br />消亡之前要自动执行析构函数。由于malloc/free是库函数而不是运算符，不在编译器控制权限之内，不能够把执行构造函数和析构<br />函数的任务强加于malloc/free。<br /><br />3. C++语言需要一个能完成动态内存分配和初始化工作的运算符new，以及一个能完成清理与释放内存工作的运算符delete。<br /><br /><span style="color: red"><strong>4. new/delete不是库函数，而是运算符。</strong></span></div><img src ="http://www.cppblog.com/Dream5/aggbug/161772.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Dream5/" target="_blank">梦五</a> 2011-12-08 16:18 <a href="http://www.cppblog.com/Dream5/articles/161772.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>指针大全</title><link>http://www.cppblog.com/Dream5/articles/161673.html</link><dc:creator>梦五</dc:creator><author>梦五</author><pubDate>Wed, 07 Dec 2011 09:05:00 GMT</pubDate><guid>http://www.cppblog.com/Dream5/articles/161673.html</guid><wfw:comment>http://www.cppblog.com/Dream5/comments/161673.html</wfw:comment><comments>http://www.cppblog.com/Dream5/articles/161673.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Dream5/comments/commentRss/161673.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Dream5/services/trackbacks/161673.html</trackback:ping><description><![CDATA[<div>1. 常量指针：&#8220;是常量的指针&#8221;，即指针指向一个常量。 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">5</span><span style="color: #000000">;<br /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pi&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">i;</span></div><br />2. 指针常量：&#8220;是指针的常量&#8221;，即指针存储的内存地址是一个常量，不能修改。 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">5</span><span style="color: #000000">;<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;p&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">i;</span></div><br />3. 函数指针：&#8220;指向函数的指针&#8221;，函数名是函数的首地址。 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000">//</span><span style="color: #008000">&nbsp;声明形式</span><span style="color: #008000"><br /></span><span style="color: #000000">typedef&nbsp;函数返回类型&nbsp;(</span><span style="color: #000000">*</span><span style="color: #000000">函数指针名称)(形参类型1，形参类型2<img alt="" src="http://www.cppblog.com/Images/dot.gif" />);<br /><br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;例子</span><span style="color: #008000"><br /></span><span style="color: #000000">typedef&nbsp;</span><span style="color: #0000ff">long</span><span style="color: #000000">&nbsp;(</span><span style="color: #000000">*</span><span style="color: #000000">fun)(</span><span style="color: #0000ff">int</span><span style="color: #000000">);<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;函数指针fun，指向一个返回类型为long型，参数类型为int型的函数。</span><span style="color: #008000"><br /></span><span style="color: #000000"><br /></span></div><br />4. 指针数组：&#8220;是指数组的元素是一个指针&#8221;，即数组里面装着指针。 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">ptr[</span><span style="color: #000000">4</span><span style="color: #000000">];&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;ptr是一个有4个元素，每个元素都是一个指向int指针的指针数组。<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #008000">//<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">(ptr[</span><span style="color: #000000">4</span><span style="color: #000000">]);&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;Ditto</span></div></div><br />5. 数组指针：&#8220;是指指向数组的指针&#8221;，即它是指针，指向整个数组。 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #008000">//</span><span style="color: #008000">&nbsp;ptr是一个指向整型数组的指针。</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;(</span><span style="color: #000000">*</span><span style="color: #000000">ptr)[];</span></div><br />6. 迷途指针：&#8220;也叫悬浮指针，失控指针，是当对一个指针进行delete操作后（这样会释放它所指向的内存），并没有把它设置为空时产生的&#8221;。而后，如果你没有重新赋值就试图再次使用该指针，引起的结果是不可预料的。
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">5</span><span style="color: #000000">;<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pInt&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">;<br />pInt&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">i;<br />delete&nbsp;pInt;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;此时pInt为迷途指针</span><span style="color: #008000"><br /></span><span style="color: #000000">pInt&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;NULL;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;此时PInt为空指针</span></div><br /><img src ="http://www.cppblog.com/Dream5/aggbug/161673.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Dream5/" target="_blank">梦五</a> 2011-12-07 17:05 <a href="http://www.cppblog.com/Dream5/articles/161673.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++位域</title><link>http://www.cppblog.com/Dream5/articles/148685.html</link><dc:creator>梦五</dc:creator><author>梦五</author><pubDate>Tue, 14 Jun 2011 15:57:00 GMT</pubDate><guid>http://www.cppblog.com/Dream5/articles/148685.html</guid><wfw:comment>http://www.cppblog.com/Dream5/comments/148685.html</wfw:comment><comments>http://www.cppblog.com/Dream5/articles/148685.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Dream5/comments/commentRss/148685.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Dream5/services/trackbacks/148685.html</trackback:ping><description><![CDATA[<div><span style="font-family: Lucida Console">引子：</span><br /><span style="font-family: Lucida Console">有些信息在存储时，并不需要占用一个完整的字节（1字节=8bit）,而只需占几个或一个二进制位。例如在存放一个开关变量时，只有0和1 两种状态， 用1位二进制位存储即可。为了节省存储空间，并使处理简便，Ｃ语言又提供了一种数据结构，称为&#8220;位域&#8221;或&#8220;位段&#8221;。</span><br /><span style="font-family: Lucida Console">所谓&#8220;位域&#8221;是把一个字节中的二进制位划分为几个不同的区域， 并说明每个区域的位数。每个域有一个域名，允许在程序中按域名进行操作。 这样就可以把几个不同的对象用一个字节的二进制位域来表示。</span><br /><span style="font-family: Lucida Console">一、位域的定义和位域变量的说明</span><br /><span style="font-family: Lucida Console">位域定义与结构定义相仿，其形式为：</span><br /><span style="font-family: Lucida Console">struct 位域结构名 </span><br /><span style="font-family: Lucida Console">{ </span><br /><span style="font-family: Lucida Console">&nbsp; 位域列表 </span><br /><span style="font-family: Lucida Console">}; </span><br /><span style="font-family: Lucida Console">其中位域列表的形式为： 类型说明符 位域名:位域长度;</span><br /><span style="font-family: Lucida Console">如：</span> 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="font-family: Lucida Console; color: #0000ff">struct</span><span style="font-family: Lucida Console; color: #000000">&nbsp;bs<br /></span><span style="font-family: Lucida Console; color: #000000">{<br />&nbsp;&nbsp;</span><span style="font-family: Lucida Console; color: #0000ff">int</span><span style="font-family: Lucida Console; color: #000000">&nbsp;a:</span><span style="font-family: Lucida Console; color: #000000">8</span><span style="font-family: Lucida Console; color: #000000">;<br />&nbsp;&nbsp;</span><span style="font-family: Lucida Console; color: #0000ff">int</span><span style="font-family: Lucida Console; color: #000000">&nbsp;b:</span><span style="font-family: Lucida Console; color: #000000">2</span><span style="font-family: Lucida Console; color: #000000">;<br />&nbsp;&nbsp;</span><span style="font-family: Lucida Console; color: #0000ff">int</span><span style="font-family: Lucida Console; color: #000000">&nbsp;c:</span><span style="font-family: Lucida Console; color: #000000">6</span><span style="font-family: Lucida Console; color: #000000">;<br /></span><span style="font-family: Lucida Console; color: #000000">};</span></div><span style="font-family: Lucida Console">位域变量的说明方式与结构体类型变量的说明方式相同，可采用先定义后说明、同时定义说明或直接说明这三种方式。例如：</span> 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="font-family: Lucida Console; color: #0000ff">struct</span><span style="font-family: Lucida Console; color: #000000">&nbsp;bs<br /></span><span style="font-family: Lucida Console; color: #000000">{<br />&nbsp;&nbsp;</span><span style="font-family: Lucida Console; color: #0000ff">int</span><span style="font-family: Lucida Console; color: #000000">&nbsp;a:</span><span style="font-family: Lucida Console; color: #000000">8</span><span style="font-family: Lucida Console; color: #000000">;<br />&nbsp;&nbsp;</span><span style="font-family: Lucida Console; color: #0000ff">int</span><span style="font-family: Lucida Console; color: #000000">&nbsp;b:</span><span style="font-family: Lucida Console; color: #000000">2</span><span style="font-family: Lucida Console; color: #000000">;<br />&nbsp;&nbsp;</span><span style="font-family: Lucida Console; color: #0000ff">int</span><span style="font-family: Lucida Console; color: #000000">&nbsp;c:</span><span style="font-family: Lucida Console; color: #000000">6</span><span style="font-family: Lucida Console; color: #000000">;<br /></span><span style="font-family: Lucida Console; color: #000000">}data;</span></div><span style="font-family: Lucida Console">data为bs类型的变量，共占两个字节的空间。其中位域a占8位、位域b占2位、位域c占6位。</span><br /><br /><span style="font-family: Lucida Console">备注：</span><br /><strong style="font-family: Lucida Console">1. &nbsp;<span style="color: #ff0000">一个位域</span><span style="color: #ff0000">必须存储在同一个字节中，不能跨两</span></strong><span style="font-family: Lucida Console; color: rgb(255,0,0)">个字节</span><span style="font-family: Lucida Console">。如一个字节所剩空间不够存放另一位域时，应从下一单元起存放该位域。也可以有意使某位域从下一单元开始,例如</span> 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-family: Lucida Console; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;bs<br />{<br />&nbsp;&nbsp;unsigned&nbsp;a:</span><span style="color: #000000">4</span><span style="color: #000000">;<br />&nbsp;&nbsp;unsigned&nbsp;&nbsp;:</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;空域</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;unsigned&nbsp;b:</span><span style="color: #000000">4</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;从下一字节单元开始存放</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;unsigned&nbsp;c:</span><span style="color: #000000">4</span><span style="color: #000000">;<br />};</span></div><span style="font-family: Lucida Console">在这个位域定义中，位域a占第一字节的4位，后4位填0表示不使用；位域b从第二字节开始，占用4位，位域c占用4位。</span><br style="font-family: Lucida Console" /><br style="font-family: Lucida Console" />
<p style="font-family: Lucida Console">2. 由于位域不允许跨两个字节，因此位域的长度不能大于1个字节的长度，也就是说不能超过8位二进制位。<br /><br />3. 位域可以无位域名，这时它只用来作填充或调整位置。无名的位域是不能使用的。例如：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-family: Lucida Console; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;k<br />{<br />&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;a:</span><span style="color: #000000">1</span><span style="color: #000000">;<br />&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;&nbsp;:</span><span style="color: #000000">2</span><span style="color: #000000">;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;该2位不能使用</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;b:</span><span style="color: #000000">3</span><span style="color: #000000">;</span></div>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #000000"><br />&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;c:</span><span style="color: #000000">2</span><span style="color: #000000">;<br />};</span></div>
<p style="font-family: Times New Roman">位域在本质上就是一种结构类型，不过其成员是按二进制位分配的。<br /></p><br />二&nbsp; 位域的使用<br />位域的使用和结构体成员的使用相同，其一般形式为位域变量名.位域名<br />位域允许用各种格式输出：<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_11_340_Open_Image" onclick="this.style.display='none'; Codehighlighter1_11_340_Open_Text.style.display='none'; Codehighlighter1_11_340_Closed_Image.style.display='inline'; Codehighlighter1_11_340_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_11_340_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_11_340_Closed_Text.style.display='none'; Codehighlighter1_11_340_Open_Image.style.display='inline'; Codehighlighter1_11_340_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_11_340_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_11_340_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;typedef&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;bs<br /><img id="Codehighlighter1_35_93_Open_Image" onclick="this.style.display='none'; Codehighlighter1_35_93_Open_Text.style.display='none'; Codehighlighter1_35_93_Closed_Image.style.display='inline'; Codehighlighter1_35_93_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_35_93_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_35_93_Closed_Text.style.display='none'; Codehighlighter1_35_93_Open_Image.style.display='inline'; Codehighlighter1_35_93_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_35_93_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_35_93_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;a:</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;b:</span><span style="color: #000000">3</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;c:</span><span style="color: #000000">4</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;}</span></span><span style="color: #000000">s_bs,</span><span style="color: #000000">*</span><span style="color: #000000">pbs;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;s_bs&nbsp;bs1;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;pbs&nbsp;pbs1;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;bs1.a&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;bs1.b&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">7</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;bs1.c&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">15</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d,%d,%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,bs1.a,bs1.b,bs1.c);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;pbs1&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">bs1;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;pbs1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">a&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;pbs1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">b&nbsp;&amp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;pbs1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">c&nbsp;|</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">3</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d,%d,%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,pbs1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">a,pbs1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">b,pbs1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">c);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div>注意：对位域的赋值不能超过其允许的范围.<br /><br />含位域结构体的sizeof<br />位域成员不能单独被取sizeof值。<br />C99规定int、unsigned int和bool可以作为位域类型，但编译器几乎都对此作了扩展，允许其它类型类型的存在。<br /><br />使用位域的主要目的是压缩存储，其大致规则为：<br />1. 如果相邻位域字段的类型相同，且其位宽之和小于类型的sizeof大小，则后面的字段将紧邻前一个字段存储，直到不能容纳为止；<br />2.&nbsp;如果相邻位域字段的类型相同，但其位宽之和大于类型的sizeof大小，则后面的字段将从新的存储单元开始，其偏移量为其类型大小的整数倍；<br />3.&nbsp;如果相邻的位域字段的类型不同，则各编译器的具体实现有差异，VC6采取不压缩方式，Dev-C++采取压缩方式；<br />4.&nbsp;如果位域字段之间穿插着非位域字段，则不进行压缩；<br />5.&nbsp;整个结构体的总大小为最宽基本类型成员大小的整数倍。<br /><br />示例<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff">struct</span><span style="color: #000000"><br /><img id="Codehighlighter1_7_48_Open_Image" onclick="this.style.display='none'; Codehighlighter1_7_48_Open_Text.style.display='none'; Codehighlighter1_7_48_Closed_Image.style.display='inline'; Codehighlighter1_7_48_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_7_48_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_7_48_Closed_Text.style.display='none'; Codehighlighter1_7_48_Open_Image.style.display='inline'; Codehighlighter1_7_48_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_7_48_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_7_48_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;f1:</span><span style="color: #000000">3</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;f2:</span><span style="color: #000000">4</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;f3:</span><span style="color: #000000">5</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000">;</span></div>其内存布局为：<br /><br /><br /><br /></div><img src ="http://www.cppblog.com/Dream5/aggbug/148685.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Dream5/" target="_blank">梦五</a> 2011-06-14 23:57 <a href="http://www.cppblog.com/Dream5/articles/148685.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>柔性数组成员</title><link>http://www.cppblog.com/Dream5/articles/148386.html</link><dc:creator>梦五</dc:creator><author>梦五</author><pubDate>Thu, 09 Jun 2011 16:46:00 GMT</pubDate><guid>http://www.cppblog.com/Dream5/articles/148386.html</guid><wfw:comment>http://www.cppblog.com/Dream5/comments/148386.html</wfw:comment><comments>http://www.cppblog.com/Dream5/articles/148386.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Dream5/comments/commentRss/148386.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Dream5/services/trackbacks/148386.html</trackback:ping><description><![CDATA[<p><span style="font-family: Times New Roman"><span style="font-family: Times New Roman; font-size: 14pt">转自：<br /><a href="http://www.aitilife.com/post/2011/04/10/array-and-point-art-section-eight.aspx"><span style="font-family: Times New Roman; font-size: 14pt">http://www.aitilife.com/post/2011/04/10/array-and-point-art-section-eight.aspx</span></a><br /><br /><span style="font-family: Times New Roman; font-size: 14pt">-</span></span><span style="font-family: Times New Roman; font-size: 14pt">不完整类型(incomplete type)<br /></span><span style="font-family: Times New Roman; font-size: 14pt">C和C++对于不完整类型的定义是一样的。<br /></span><span style="font-family: Times New Roman; font-size: 14pt">不完整类型是这样一种类型，它缺乏足够的信息例如长度去描述一个完整的对象。<br /></span><span style="font-family: Times New Roman; font-size: 14pt">incomplete types : types that describe objects but lack information needed to be determine their sizes</span><span style="font-family: Courier; font-size: 14pt"><br /><br /></span><span style="font-family: Times New Roman; font-size: 14pt">不完整类型举例：</span><span style="font-family: Courier"><br /></span><span style="font-family: Times New Roman; font-size: 14pt">前向声明就是一种常用的不完整类型</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">class</span><span style="color: #000000">&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">base</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">;<br /><br /></span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">struct</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;test;</span></div>
<p><span style="font-family: Times New Roman; font-size: 14pt">base 和 test 只给出了声明，没有给出定义。</span><br /><span style="font-family: Times New Roman; font-size: 14pt">不完整数据类型必须通过某种方式补充完整，才能使它们进行实例化。否则只能用于定义指针或引用，因为此时实例化的是指针或引用本身，不是base和test对象。</span><br /><span style="font-family: Times New Roman; font-size: 14pt">一个未知长度的数组也属于不完整类型：</span></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">extern</span><span style="color: #000000">&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">int</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;a[];</span></div>
<p><span style="font-family: Times New Roman; font-size: 14pt">extern 关键字不能去掉，因为数组的长度未知，不能作为定义出现。不完整类型的数组需要补充完整才能使用。</span><br /><span style="font-family: Times New Roman; font-size: 14pt">不完整类型的数组可以通过几种方式补充完整，大括号形式的初始化就是其中的一种方式：</span></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img id="Codehighlighter1_11_19_Open_Image" onclick="this.style.display='none'; Codehighlighter1_11_19_Open_Text.style.display='none'; Codehighlighter1_11_19_Closed_Image.style.display='inline'; Codehighlighter1_11_19_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_11_19_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_11_19_Closed_Text.style.display='none'; Codehighlighter1_11_19_Open_Image.style.display='inline'; Codehighlighter1_11_19_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">int</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;ia[]&nbsp;</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">=</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_11_19_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_11_19_Open_Text"><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">{&nbsp;</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">10</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">,</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">20</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;}</span></span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">;</span></div>
<p><br /><span style="font-family: Times New Roman; font-size: 14pt">柔性数组成员（flexible array member）也叫伸缩性数组成员，它的出现反映了C程序员对精炼代码的极致追求。这种代码结构产生于对动态结构体的需求。在日常的编程中，有时候需要在结构体中存放一个长度动态的字符串，一般的做法，是在结构体中定义一个指针成员，这个指针成员指向该字符串所在的动态内存空间，例如：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">struct</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;s_test<br /><img id="Codehighlighter1_14_48_Open_Image" onclick="this.style.display='none'; Codehighlighter1_14_48_Open_Text.style.display='none'; Codehighlighter1_14_48_Closed_Image.style.display='inline'; Codehighlighter1_14_48_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_14_48_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_14_48_Closed_Text.style.display='none'; Codehighlighter1_14_48_Open_Image.style.display='inline'; Codehighlighter1_14_48_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_14_48_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_14_48_Open_Text"><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">int</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;a;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">double</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;b;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">char</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">*</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;p;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">}</span></span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">;</span></div>
<p><span style="font-family: Times New Roman; font-size: 14pt">p指向字符串，这种方法造成字符串与结构体是分离的，不利于操作。把字符串和结构体连在一起的话，效果会更好，可以修改如下：</span></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">char</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;a[]&nbsp;</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">=</span><span style="color: #000000">&nbsp;</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">"</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">Hello&nbsp;world</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">"</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">;<br /></span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">struct</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;s_test&nbsp;</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">*</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">ptest&nbsp;</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">=</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;(</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">struct</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;s_test</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">*</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">)malloc&nbsp;(&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">sizeof</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">(s_test)</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">+</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">streln(a)</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">+</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">1</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;);<br /></span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">strcpy(&nbsp;ptest</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">+</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">1</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">,a&nbsp;);</span></div>
<p><span style="font-family: Times New Roman; font-size: 14pt">这样一来，( char* )( ptestt + 1 )就是字符串&#8220;hello world&#8221;的地址了。这时候p成了多余的东西，可以去掉。但是，又产生了另外一个问题：老是使用( char* )( ptest + 1 )不方便。如果能够找出一种方法，既能直接引用该字符串，又不占用结构体的空间，就完美了，符合这种条件的代码结构应该是一个非对象的符号地址，在结构体的尾部放置一个0长度的数组是一个绝妙的解决方案。不过，C/C++标准规定不能定义长度为0的数组，因此，有些编译器就把0长度的数组成员作为自己的非标准扩展，例如：</span></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">struct</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;s_test2<br /><img id="Codehighlighter1_15_51_Open_Image" onclick="this.style.display='none'; Codehighlighter1_15_51_Open_Text.style.display='none'; Codehighlighter1_15_51_Closed_Image.style.display='inline'; Codehighlighter1_15_51_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_15_51_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_15_51_Closed_Text.style.display='none'; Codehighlighter1_15_51_Open_Image.style.display='inline'; Codehighlighter1_15_51_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_15_51_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_15_51_Open_Text"><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">int</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;a;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">double</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;b;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">char</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;c[</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">0</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">}</span></span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">;</span></div>
<p><span style="font-family: Times New Roman; font-size: 14pt">c就叫柔性数组成员，如果把ptest指向的动态分配内存看作一个整体，c就是一个长度可以动态变化的结构体成员，柔性一词来源于此。c的长度为0，因此它不占用test的空间，同时ptest-&gt;c就是&#8220;hello world&#8221;的首地址，不需要再使用( char* )( PntTest + 1 )这么丑陋的语法了。</span><br /></p>
<p style="text-align: justify; padding-bottom: 0px; margin: 0px 0px 1.5em; padding-left: 0px; outline-width: 0px; padding-right: 0px; font-size: 14px; padding-top: 0px"><span style="font-family: Times New Roman; font-size: 14pt">鉴于这种代码结构所产生的重要作用，C99甚至把它收入了标准中：</span></p>
<p><span style="font-family: Times New Roman; font-size: 14pt">As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member.</span><br /><span style="font-family: Times New Roman; font-size: 14pt">C99使用不完整类型实现柔性数组成员，标准形式是这样的：</span></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">struct</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;s_test<br /><img id="Codehighlighter1_14_49_Open_Image" onclick="this.style.display='none'; Codehighlighter1_14_49_Open_Text.style.display='none'; Codehighlighter1_14_49_Closed_Image.style.display='inline'; Codehighlighter1_14_49_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_14_49_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_14_49_Closed_Text.style.display='none'; Codehighlighter1_14_49_Open_Image.style.display='inline'; Codehighlighter1_14_49_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_14_49_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_14_49_Open_Text"><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">int</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;a;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">double</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;b;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">char</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;c[];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">}</span></span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span></div>
<p><span style="font-family: Times New Roman; font-size: 14pt">c同样不占用test的空间，只作为一个符号地址存在，而且必须是结构体的最后一个成员。柔性数组成员不仅可以用于字符数组，还可以是元素为其它类型的数组，例如：</span></p>
<p>&nbsp;</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">struct</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;s_test<br /><img id="Codehighlighter1_14_48_Open_Image" onclick="this.style.display='none'; Codehighlighter1_14_48_Open_Text.style.display='none'; Codehighlighter1_14_48_Closed_Image.style.display='inline'; Codehighlighter1_14_48_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_14_48_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_14_48_Closed_Text.style.display='none'; Codehighlighter1_14_48_Open_Image.style.display='inline'; Codehighlighter1_14_48_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_14_48_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_14_48_Open_Text"><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">int</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;a;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">double</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">&nbsp;b;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="font-family: Times New Roman; color: #0000ff; font-size: 14pt">float</span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">[];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">}</span></span><span style="font-family: Times New Roman; color: #000000; font-size: 14pt">;</span></div>
<p><span style="font-family: Times New Roman; font-size: 14pt">应当尽量使用标准形式，在非C99的场合，可以使用指针方法。有些人使用char c[1]，这是非常不可取的，把这样的a用作柔性数组成员会发生越界行为，虽然C/C++标准并没有规定编译器应当检查越界，但也没有规定不能检查越界，为了一个小小的指针空间而牺牲移植性，是不值得的。<br /></span></p>
<p>示例代码：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">//</span><span style="color: #008000">&nbsp;C/C++结构体柔性数组成员</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">malloc.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />typedef&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;s_test<br /><img id="Codehighlighter1_104_156_Open_Image" onclick="this.style.display='none'; Codehighlighter1_104_156_Open_Text.style.display='none'; Codehighlighter1_104_156_Closed_Image.style.display='inline'; Codehighlighter1_104_156_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_104_156_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_104_156_Closed_Text.style.display='none'; Codehighlighter1_104_156_Open_Image.style.display='inline'; Codehighlighter1_104_156_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_104_156_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_104_156_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;b;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;ch[</span><span style="color: #000000">0</span><span style="color: #000000">];&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;or&nbsp;char&nbsp;ch[];</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" /></span><span style="color: #000000">}</span></span><span style="color: #000000">st,</span><span style="color: #000000">*</span><span style="color: #000000">pst;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_178_832_Open_Image" onclick="this.style.display='none'; Codehighlighter1_178_832_Open_Text.style.display='none'; Codehighlighter1_178_832_Closed_Image.style.display='inline'; Codehighlighter1_178_832_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_178_832_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_178_832_Closed_Text.style.display='none'; Codehighlighter1_178_832_Open_Image.style.display='inline'; Codehighlighter1_178_832_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_178_832_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_178_832_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;ch1[]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Hello&nbsp;world!</span><span style="color: #000000">"</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;ch2[]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">This&nbsp;is&nbsp;a&nbsp;test,i&nbsp;love&nbsp;Arsenal!</span><span style="color: #000000">"</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;pst&nbsp;pstest1&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;(s_test</span><span style="color: #000000">*</span><span style="color: #000000">)malloc(&nbsp;</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(s_test)&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;strlen(ch1)&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(&nbsp;NULL&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;pstest1&nbsp;)<br /><img id="Codehighlighter1_352_419_Open_Image" onclick="this.style.display='none'; Codehighlighter1_352_419_Open_Text.style.display='none'; Codehighlighter1_352_419_Closed_Image.style.display='inline'; Codehighlighter1_352_419_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_352_419_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_352_419_Closed_Text.style.display='none'; Codehighlighter1_352_419_Open_Image.style.display='inline'; Codehighlighter1_352_419_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_352_419_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_352_419_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pstest1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pstest1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">b&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">11</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(pstest1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">ch,ch1);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">pstest1:&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;pstest1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">i&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;pstest1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">b<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;pstest1</span><span style="color: #000000">-&gt;</span><span style="color: #000000">ch&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;pst&nbsp;pstest2&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;(pst)malloc(&nbsp;</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(st)&nbsp;&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">strlen(ch2)&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(&nbsp;NULL&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;pstest2&nbsp;)<br /><img id="Codehighlighter1_614_681_Open_Image" onclick="this.style.display='none'; Codehighlighter1_614_681_Open_Text.style.display='none'; Codehighlighter1_614_681_Closed_Image.style.display='inline'; Codehighlighter1_614_681_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_614_681_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_614_681_Closed_Text.style.display='none'; Codehighlighter1_614_681_Open_Image.style.display='inline'; Codehighlighter1_614_681_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_614_681_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_614_681_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pstest2</span><span style="color: #000000">-&gt;</span><span style="color: #000000">i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pstest2</span><span style="color: #000000">-&gt;</span><span style="color: #000000">b&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">22</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(pstest2</span><span style="color: #000000">-&gt;</span><span style="color: #000000">ch,ch2);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">pstest2:&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;pstest2</span><span style="color: #000000">-&gt;</span><span style="color: #000000">i&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;pstest2</span><span style="color: #000000">-&gt;</span><span style="color: #000000">b<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;pstest2</span><span style="color: #000000">-&gt;</span><span style="color: #000000">ch&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;free(&nbsp;pstest1&nbsp;);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;free(&nbsp;pstest2&nbsp;);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div>
<p><br /><br />&nbsp;</p>
<p><br /><br /></span><br /><br /><br /><br /><br /><br /><br /><br /></span></span>&nbsp;</p><img src ="http://www.cppblog.com/Dream5/aggbug/148386.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Dream5/" target="_blank">梦五</a> 2011-06-10 00:46 <a href="http://www.cppblog.com/Dream5/articles/148386.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>"常量指针" 和 "指针常量"</title><link>http://www.cppblog.com/Dream5/articles/145167.html</link><dc:creator>梦五</dc:creator><author>梦五</author><pubDate>Wed, 27 Apr 2011 12:21:00 GMT</pubDate><guid>http://www.cppblog.com/Dream5/articles/145167.html</guid><wfw:comment>http://www.cppblog.com/Dream5/comments/145167.html</wfw:comment><comments>http://www.cppblog.com/Dream5/articles/145167.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Dream5/comments/commentRss/145167.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Dream5/services/trackbacks/145167.html</trackback:ping><description><![CDATA[<span style="font-family: 微软雅黑"> 
<p>常量指针：&#8220;是常量的指针&#8221;，即指针指向一个常量。<br />要注意的两点：<br />1. 常量指针，指针指向的是一个常量，意思是说，存储在"指针存储的内存里"的内容是一个常量，所以不能修改常量指针指向的内存地址里内容。<br /><br />2. 常量指针指向（存储）的内存地址可以修改。<br /><br />示例代码：<br /></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img id="Codehighlighter1_53_455_Open_Image" onclick="this.style.display='none'; Codehighlighter1_53_455_Open_Text.style.display='none'; Codehighlighter1_53_455_Closed_Image.style.display='inline'; Codehighlighter1_53_455_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_53_455_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_53_455_Closed_Text.style.display='none'; Codehighlighter1_53_455_Open_Image.style.display='inline'; Codehighlighter1_53_455_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_53_455_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_53_455_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;iConst&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">5</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;iConst2&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">20</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">10</span><span style="color: #000000"><img id="Codehighlighter1_94_107_Open_Image" onclick="this.style.display='none'; Codehighlighter1_94_107_Open_Text.style.display='none'; Codehighlighter1_94_107_Closed_Image.style.display='inline'; Codehighlighter1_94_107_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_94_107_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_94_107_Closed_Text.style.display='none'; Codehighlighter1_94_107_Open_Image.style.display='inline'; Codehighlighter1_94_107_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_94_107_Closed_Text">/**/</span><span id="Codehighlighter1_94_107_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">&nbsp;声明一个常量指针&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;p&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">iConst;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;这里p是一个指向常量的指针，即存储在&#8220;指针p指向（存储）的内存地址&#8221;里的内容是一个常量</span><span style="color: #008000"><br /></span><span style="color: #008080">12</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000"><br /></span><span style="color: #008080">13</span><span style="color: #000000"><img id="Codehighlighter1_184_218_Open_Image" onclick="this.style.display='none'; Codehighlighter1_184_218_Open_Text.style.display='none'; Codehighlighter1_184_218_Closed_Image.style.display='inline'; Codehighlighter1_184_218_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_184_218_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_184_218_Closed_Text.style.display='none'; Codehighlighter1_184_218_Open_Image.style.display='inline'; Codehighlighter1_184_218_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_184_218_Closed_Text">/**/</span><span id="Codehighlighter1_184_218_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">&nbsp;解除指针的引用，取出存储在&#8220;指针p指向的内存地址&#8221;里的内容&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;OK,output&nbsp;5.</span><span style="color: #008000"><br /></span><span style="color: #008080">15</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img id="Codehighlighter1_265_293_Open_Image" onclick="this.style.display='none'; Codehighlighter1_265_293_Open_Text.style.display='none'; Codehighlighter1_265_293_Closed_Image.style.display='inline'; Codehighlighter1_265_293_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_265_293_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_265_293_Closed_Text.style.display='none'; Codehighlighter1_265_293_Open_Image.style.display='inline'; Codehighlighter1_265_293_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_265_293_Closed_Text">/**/</span><span id="Codehighlighter1_265_293_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">&nbsp;试图修改存储在&#8220;指针p指向的内存地址&#8221;里的内容&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">10</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;error!因为指针p指向的内存地址里的内容是一个常量，不可以修改.</span><span style="color: #008000"><br /></span><span style="color: #008080">18</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000"><br /></span><span style="color: #008080">19</span><span style="color: #000000"><img id="Codehighlighter1_361_381_Open_Image" onclick="this.style.display='none'; Codehighlighter1_361_381_Open_Text.style.display='none'; Codehighlighter1_361_381_Closed_Image.style.display='inline'; Codehighlighter1_361_381_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_361_381_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_361_381_Closed_Text.style.display='none'; Codehighlighter1_361_381_Open_Image.style.display='inline'; Codehighlighter1_361_381_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_361_381_Closed_Text">/**/</span><span id="Codehighlighter1_361_381_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">&nbsp;常量指针指向的内存地址可以修改&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">20</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">iConst2;<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">22</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;OK,output&nbsp;20.</span><span style="color: #008000"><br /></span><span style="color: #008080">23</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">25</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div>上述代码，如果error没有注释掉，编译器在编译的时候会指出一个错误，如下：<br /><span style="color: #000000">&nbsp;<span style="color: #ff0000">error C2166: l-value specifies const object<br /></span><br />指针常量：&#8220;是指针的常量&#8221;，即指针存储的内存地址是一个常量，不能修改。<br />要注意的两点：<br />1. 指针常量，即指向（存储）的内存地址是一个常量的指针，它指向的内存地址是一个常量不能修改。<br />2. 存储在&#8220;指针常量指向（存储）&#8221;的内存地址里的内容，不是常量，可以修改.<br /><br />示例代码：<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img id="Codehighlighter1_53_517_Open_Image" onclick="this.style.display='none'; Codehighlighter1_53_517_Open_Text.style.display='none'; Codehighlighter1_53_517_Closed_Image.style.display='inline'; Codehighlighter1_53_517_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_53_517_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_53_517_Closed_Text.style.display='none'; Codehighlighter1_53_517_Open_Image.style.display='inline'; Codehighlighter1_53_517_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_53_517_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_53_517_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;iConst&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">5</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;iConst2&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">20</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img id="Codehighlighter1_93_106_Open_Image" onclick="this.style.display='none'; Codehighlighter1_93_106_Open_Text.style.display='none'; Codehighlighter1_93_106_Closed_Image.style.display='inline'; Codehighlighter1_93_106_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_93_106_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_93_106_Closed_Text.style.display='none'; Codehighlighter1_93_106_Open_Image.style.display='inline'; Codehighlighter1_93_106_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_93_106_Closed_Text">/**/</span><span id="Codehighlighter1_93_106_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">&nbsp;声明一个指针常量&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;p&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">iConst;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;这里p是一个指针常量，即存储在&#8220;指针p指向（存储）的内存地址&#8221;是一个常量</span><span style="color: #008000"><br /></span><span style="color: #008080">11</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000"><br /></span><span style="color: #008080">12</span><span style="color: #000000"><img id="Codehighlighter1_176_210_Open_Image" onclick="this.style.display='none'; Codehighlighter1_176_210_Open_Text.style.display='none'; Codehighlighter1_176_210_Closed_Image.style.display='inline'; Codehighlighter1_176_210_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_176_210_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_176_210_Closed_Text.style.display='none'; Codehighlighter1_176_210_Open_Image.style.display='inline'; Codehighlighter1_176_210_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_176_210_Closed_Text">/**/</span><span id="Codehighlighter1_176_210_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">&nbsp;解除指针的引用，取出存储在&#8220;指针p指向的内存地址&#8221;里的内容&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;OK,output&nbsp;5.</span><span style="color: #008000"><br /></span><span style="color: #008080">14</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img id="Codehighlighter1_257_278_Open_Image" onclick="this.style.display='none'; Codehighlighter1_257_278_Open_Text.style.display='none'; Codehighlighter1_257_278_Closed_Image.style.display='inline'; Codehighlighter1_257_278_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_257_278_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_257_278_Closed_Text.style.display='none'; Codehighlighter1_257_278_Open_Image.style.display='inline'; Codehighlighter1_257_278_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_257_278_Closed_Text">/**/</span><span id="Codehighlighter1_257_278_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">&nbsp;试图修改&#8220;指针p指向的内存地址&#8220;&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">iConst2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;error!因为指针p指向的内存地址是一个常量，不可以修改.</span><span style="color: #008000"><br /></span><span style="color: #008080">17</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000"><br /></span><span style="color: #008080">18</span><span style="color: #000000"><img id="Codehighlighter1_347_375_Open_Image" onclick="this.style.display='none'; Codehighlighter1_347_375_Open_Text.style.display='none'; Codehighlighter1_347_375_Closed_Image.style.display='inline'; Codehighlighter1_347_375_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_347_375_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_347_375_Closed_Text.style.display='none'; Codehighlighter1_347_375_Open_Image.style.display='inline'; Codehighlighter1_347_375_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_347_375_Closed_Text">/**/</span><span id="Codehighlighter1_347_375_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">&nbsp;指针常量指向的内存地址的内容不是常量，可以修改&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">19</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">33</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;OK.&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">20</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;OK,output&nbsp;33;</span><span style="color: #008000"><br /></span><span style="color: #008080">21</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;iConst&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;OK,&nbsp;output&nbsp;33;</span><span style="color: #008000"><br /></span><span style="color: #008080">22</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">23</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div>同样地，上述代码，如果error没有注释掉，编译器在编译的时候会指出一个错误，如下：<br /><span style="color: #000000">&nbsp;<span style="color: #ff0000">error C2166: l-value specifies const object<br /></span></span><br /><br /></span></span><img src ="http://www.cppblog.com/Dream5/aggbug/145167.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Dream5/" target="_blank">梦五</a> 2011-04-27 20:21 <a href="http://www.cppblog.com/Dream5/articles/145167.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>指针陷阱知多少...</title><link>http://www.cppblog.com/Dream5/articles/145086.html</link><dc:creator>梦五</dc:creator><author>梦五</author><pubDate>Tue, 26 Apr 2011 13:45:00 GMT</pubDate><guid>http://www.cppblog.com/Dream5/articles/145086.html</guid><wfw:comment>http://www.cppblog.com/Dream5/comments/145086.html</wfw:comment><comments>http://www.cppblog.com/Dream5/articles/145086.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Dream5/comments/commentRss/145086.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Dream5/services/trackbacks/145086.html</trackback:ping><description><![CDATA[<span style="FONT-FAMILY: 微软雅黑">指针，对于C/C++的程序员来说是永远不能回避的，用得好，会让你的代码更有效率和更简洁。<br>但是稍有不慎，你就会进入它的各种陷阱中，而变得茫然不知所措。<br>本文就简要说说在编程过程中容易遇到的几种指针陷阱。<br><br>指针定义<br>指针是一种特殊类型的变量，它存储的是"变量在计算机内存中的存放位置"，也即内存地址。<br>我们通常说，指针指向一块内存地址，和指针存放的是一块内存地址表达的是同样的意思。<br><br>陷阱1——&#8220;偷天换日&#8221;<br>描述：指针最经常用的一种用途是，用来存放（指向）我们在堆中动态分配的内存地址，我们都知道，从堆中动态分配的内存地址，在使用完毕以后，需要我们自己手动去释放，这就是我们经常所说的 new —— delete&nbsp;&nbsp;&nbsp; malloc&nbsp;—— free 要成对出现的原因，因为如果我们没有主动去释放那些不再使用的动态分配的内存地址，那么在程序运行过程中会造成内存泄漏，造成资源浪费。当然，当整个程序结束以后，系统会去回收那些没有释放掉的内存。 <br>就这个简单的过程，很多时候，我们不小心就会造成&#8220;野指针&#8221;的出现，然后那些动态分配的内存无法释放，其中一个很重要的原因，就是当初用来指向动态分配的内存的指针，它的指向改变了，也就是说它指向了其他的内存块，造成原来指向的内存丢失，我们称它为"偷天换日"<br>如例1所示：<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img id=Codehighlighter1_53_366_Open_Image onclick="this.style.display='none'; Codehighlighter1_53_366_Open_Text.style.display='none'; Codehighlighter1_53_366_Closed_Image.style.display='inline'; Codehighlighter1_53_366_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_53_366_Closed_Image onclick="this.style.display='none'; Codehighlighter1_53_366_Closed_Text.style.display='none'; Codehighlighter1_53_366_Open_Image.style.display='inline'; Codehighlighter1_53_366_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_53_366_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_53_366_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img id=Codehighlighter1_68_78_Open_Image onclick="this.style.display='none'; Codehighlighter1_68_78_Open_Text.style.display='none'; Codehighlighter1_68_78_Closed_Image.style.display='inline'; Codehighlighter1_68_78_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_68_78_Closed_Image onclick="this.style.display='none'; Codehighlighter1_68_78_Closed_Text.style.display='none'; Codehighlighter1_68_78_Open_Image.style.display='inline'; Codehighlighter1_68_78_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ia[</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_68_78_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_68_78_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img id=Codehighlighter1_83_122_Open_Image onclick="this.style.display='none'; Codehighlighter1_83_122_Open_Text.style.display='none'; Codehighlighter1_83_122_Closed_Image.style.display='inline'; Codehighlighter1_83_122_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_83_122_Closed_Image onclick="this.style.display='none'; Codehighlighter1_83_122_Closed_Text.style.display='none'; Codehighlighter1_83_122_Open_Image.style.display='inline'; Codehighlighter1_83_122_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_83_122_Closed_Text>/**/</span><span id=Codehighlighter1_83_122_Open_Text><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;指针pi指向在堆中分配的内存的首地址，该内存大小为5*4=20字节.&nbsp;</span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;pi&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">];&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_152_222_Open_Image onclick="this.style.display='none'; Codehighlighter1_152_222_Open_Text.style.display='none'; Codehighlighter1_152_222_Closed_Image.style.display='inline'; Codehighlighter1_152_222_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_152_222_Closed_Image onclick="this.style.display='none'; Codehighlighter1_152_222_Closed_Text.style.display='none'; Codehighlighter1_152_222_Open_Image.style.display='inline'; Codehighlighter1_152_222_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_152_222_Closed_Text>/**/</span><span id=Codehighlighter1_152_222_Open_Text><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;这里pi指向栈中分配的数组ia的首地址，上面动态分配的20字节内存地址，<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;成为野指针，丢失无法释放，造成程序内存泄漏.&nbsp;<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;pi&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;ia;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img id=Codehighlighter1_262_291_Open_Image onclick="this.style.display='none'; Codehighlighter1_262_291_Open_Text.style.display='none'; Codehighlighter1_262_291_Closed_Image.style.display='inline'; Codehighlighter1_262_291_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_262_291_Closed_Image onclick="this.style.display='none'; Codehighlighter1_262_291_Closed_Text.style.display='none'; Codehighlighter1_262_291_Open_Image.style.display='inline'; Codehighlighter1_262_291_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_262_291_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_262_291_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(pi</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">i)&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;pi;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;这里释放的并不是最先的堆中动态分配的内存，程序运行出错.</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<p align=justify>在例1中该程序运行会出错，具体原因已经在代码注释中有标注。运行例1会出现图1所示的错误<br><img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/dream5/1.png" width=478 height=362><br>图1 出错提示<br>从图1的出错提示中我们也可以看到，他告诉我们我们在试图释放一个无效的指向堆中内存地址的指针。<br>改正方法：很明显，我们可以用一个临时指针把动态分配的内存保存一份，这样在释放的时候，我们可以利用临时指针来释放动态分配的内存地址，从而就算原来指向动态申请的内存的指针已经改变指向，我们仍然可以正确地把它释放掉，修正代码如下：　<br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_000151 onclick="this.style.display='none'; Code_Closed_Text_000151.style.display='none'; Code_Open_Image_000151.style.display='inline'; Code_Open_Text_000151.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_000151 onclick="this.style.display='none'; Code_Open_Text_000151.style.display='none'; Code_Closed_Image_000151.style.display='inline'; Code_Closed_Text_000151.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_000151></span><span style="DISPLAY: none" id=Code_Open_Text_000151><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img id=Codehighlighter1_53_402_Open_Image onclick="this.style.display='none'; Codehighlighter1_53_402_Open_Text.style.display='none'; Codehighlighter1_53_402_Closed_Image.style.display='inline'; Codehighlighter1_53_402_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_53_402_Closed_Image onclick="this.style.display='none'; Codehighlighter1_53_402_Closed_Text.style.display='none'; Codehighlighter1_53_402_Open_Image.style.display='inline'; Codehighlighter1_53_402_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_53_402_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_53_402_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img id=Codehighlighter1_68_78_Open_Image onclick="this.style.display='none'; Codehighlighter1_68_78_Open_Text.style.display='none'; Codehighlighter1_68_78_Closed_Image.style.display='inline'; Codehighlighter1_68_78_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_68_78_Closed_Image onclick="this.style.display='none'; Codehighlighter1_68_78_Closed_Text.style.display='none'; Codehighlighter1_68_78_Open_Image.style.display='inline'; Codehighlighter1_68_78_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ia[</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_68_78_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_68_78_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;pi&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">];&nbsp;&nbsp;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;ptmp&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;pi;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;用一个临时指针保存，动态分配的内存地址，以方便我们以后正确释放。</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;pi&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;ia;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;pi指向该变，但这次我们用ptmp把原来动态分配的内存地址保存起来了.</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img id=Codehighlighter1_238_267_Open_Image onclick="this.style.display='none'; Codehighlighter1_238_267_Open_Text.style.display='none'; Codehighlighter1_238_267_Closed_Image.style.display='inline'; Codehighlighter1_238_267_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_238_267_Closed_Image onclick="this.style.display='none'; Codehighlighter1_238_267_Closed_Text.style.display='none'; Codehighlighter1_238_267_Open_Image.style.display='inline'; Codehighlighter1_238_267_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_238_267_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_238_267_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(pi</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">i)&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;ptmp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;现在用临时指针把动态申请的内存地址释放，没有问题.</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;ptmp&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;NULL;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;这里可有可无，如果不是程序即将结束，把释放掉的指针赋值为NULL是一个好习惯.</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">20</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></span></div>
<p align=justify><br><br>陷阱2——"暗度陈仓"<br>描述：是的，有时候我也会犯这样的错误，像这样，我们在堆中分配一块指定大小的内存地址，并用一个指针去存储该地址，然后，我们很自然地想为这些地址赋值，或许，我们会想到用一个for循环再加上指针自增，如此赋值，我们觉得这看起来没什么错误，但是事实却有点出人意料，我们有可能不经意间该变了该指针的指向，但这次并不是明显的直接把它指向另一个内存地址，而是指针自己指向发生变化，如例2所示：<br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_53_401_Open_Image onclick="this.style.display='none'; Codehighlighter1_53_401_Open_Text.style.display='none'; Codehighlighter1_53_401_Closed_Image.style.display='inline'; Codehighlighter1_53_401_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_53_401_Closed_Image onclick="this.style.display='none'; Codehighlighter1_53_401_Closed_Text.style.display='none'; Codehighlighter1_53_401_Open_Image.style.display='inline'; Codehighlighter1_53_401_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_53_401_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_53_401_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;pi&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i&nbsp;)<br><img id=Codehighlighter1_107_183_Open_Image onclick="this.style.display='none'; Codehighlighter1_107_183_Open_Text.style.display='none'; Codehighlighter1_107_183_Closed_Image.style.display='inline'; Codehighlighter1_107_183_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_107_183_Closed_Image onclick="this.style.display='none'; Codehighlighter1_107_183_Closed_Text.style.display='none'; Codehighlighter1_107_183_Open_Image.style.display='inline'; Codehighlighter1_107_183_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_107_183_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_107_183_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">pi&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;i;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pi</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;注意这里指针pi在自增，这是一个隐藏的错误，pi已经不再是指向上面动态分配的内存的首地址了。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;)<br><img id=Codehighlighter1_214_333_Open_Image onclick="this.style.display='none'; Codehighlighter1_214_333_Open_Text.style.display='none'; Codehighlighter1_214_333_Closed_Image.style.display='inline'; Codehighlighter1_214_333_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_214_333_Closed_Image onclick="this.style.display='none'; Codehighlighter1_214_333_Closed_Text.style.display='none'; Codehighlighter1_214_333_Open_Image.style.display='inline'; Codehighlighter1_214_333_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_214_333_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_214_333_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">pi&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;会像我们想象的那样输出么？这里只会输出不可意料的数..</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pi</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;注意这里pi继续自增，现在它已经指向内存中一个未知的内存地址块了。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;pi;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;不出所料，这里释放的已经不是动态申请的内存块了<img src="http://www.cppblog.com/Images/dot.gif">所以，出错是必然的.</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<p align=justify>这里的错误提示跟例１是一样的，因为都是去释放一块并不是从堆中动态分配的内存而引起的问题。<br>改正方法：<br>方法１，跟例１一样，用一个临时指针保存动态分配的内存地址，然后释放的时候用临时指针确保释放正确。<br>方法２，赋值和取数的时候，在这里，使用数组的下标运算符的形式。代码如下：<br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_53_325_Open_Image onclick="this.style.display='none'; Codehighlighter1_53_325_Open_Text.style.display='none'; Codehighlighter1_53_325_Closed_Image.style.display='inline'; Codehighlighter1_53_325_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_53_325_Closed_Image onclick="this.style.display='none'; Codehighlighter1_53_325_Closed_Text.style.display='none'; Codehighlighter1_53_325_Open_Image.style.display='inline'; Codehighlighter1_53_325_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_53_325_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_53_325_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;pi&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i&nbsp;)<br><img id=Codehighlighter1_107_123_Open_Image onclick="this.style.display='none'; Codehighlighter1_107_123_Open_Text.style.display='none'; Codehighlighter1_107_123_Closed_Image.style.display='inline'; Codehighlighter1_107_123_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_107_123_Closed_Image onclick="this.style.display='none'; Codehighlighter1_107_123_Closed_Text.style.display='none'; Codehighlighter1_107_123_Open_Image.style.display='inline'; Codehighlighter1_107_123_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_107_123_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_107_123_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pi[i]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;i;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;)<br><img id=Codehighlighter1_154_245_Open_Image onclick="this.style.display='none'; Codehighlighter1_154_245_Open_Text.style.display='none'; Codehighlighter1_154_245_Closed_Image.style.display='inline'; Codehighlighter1_154_245_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_154_245_Closed_Image onclick="this.style.display='none'; Codehighlighter1_154_245_Closed_Text.style.display='none'; Codehighlighter1_154_245_Open_Image.style.display='inline'; Codehighlighter1_154_245_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_154_245_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_154_245_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(pi</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">j)&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;这里正确输出，跟我们想得一样<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;cout&nbsp;&lt;&lt;&nbsp;p[j]&nbsp;&lt;&lt;&nbsp;"&nbsp;";&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;这个跟上式是等价的。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;pi;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;此时pi&nbsp;仍然指向动态分配的内存的首地址，所以释放没有问题。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<p align=justify><br>陷阱３——&#8220;有名无实&#8221;<br>描述：顾名思义&#8220;有名无实&#8221;就是空有名字，其实并没有实际意义。指针也是一样，想过这样的事么，我们去动态申请一个内存，我们有没有考虑过，申请是否成功呢？或许，我们想应该是成功的，然后我们去解除一个为ＮＵＬＬ的指针的引用(pi==NULL;,*pi),很不幸这会发生不可预知的错误，而且，很难检查出来，但只能怪我们自己没有养成好的习惯，因为如果，我们每一次在申请内存的时候去判断一下是否申请成功，就可以避免这个问题了。你看，习惯也是很危险的～　<br>&nbsp;</p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;pi&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">65540</span><span style="COLOR: #000000">];&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;能成功么？或许能<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;我们应该像以下这样做，防范重于一切～～</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;NULL&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;pi&nbsp;)<br><img id=Codehighlighter1_79_127_Open_Image onclick="this.style.display='none'; Codehighlighter1_79_127_Open_Text.style.display='none'; Codehighlighter1_79_127_Closed_Image.style.display='inline'; Codehighlighter1_79_127_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_79_127_Closed_Image onclick="this.style.display='none'; Codehighlighter1_79_127_Closed_Text.style.display='none'; Codehighlighter1_79_127_Open_Image.style.display='inline'; Codehighlighter1_79_127_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_79_127_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_79_127_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">new&nbsp;a&nbsp;malloc&nbsp;fail!\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">;&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<p align=justify><br>以上...暂时这么多，以后有遇到新的再继续补充上去.<br></p>
</span>
<img src ="http://www.cppblog.com/Dream5/aggbug/145086.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Dream5/" target="_blank">梦五</a> 2011-04-26 21:45 <a href="http://www.cppblog.com/Dream5/articles/145086.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>