⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 item_099.htm

📁 C++程序编写规范,适合C++中级读者
💻 HTM
字号:
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 11">
<meta name=Originator content="Microsoft Word 11">
<link rel=File-List href="filelist-69.xml.css" tppabs="http://www.gesoftfactory.com/developer/cppcodingstandards/Item_099_files/filelist.xml">
<title>99</title>
<!--[if gte mso 9]><xml>
 <o:DocumentProperties>
  <o:Author>SGuy</o:Author>
  <o:LastAuthor>Dove</o:LastAuthor>
  <o:Revision>2</o:Revision>
  <o:TotalTime>130</o:TotalTime>
  <o:Created>2005-08-07T04:52:00Z</o:Created>
  <o:LastSaved>2005-08-07T04:52:00Z</o:LastSaved>
  <o:Pages>1</o:Pages>
  <o:Words>134</o:Words>
  <o:Characters>768</o:Characters>
  <o:Lines>6</o:Lines>
  <o:Paragraphs>1</o:Paragraphs>
  <o:CharactersWithSpaces>901</o:CharactersWithSpaces>
  <o:Version>11.5606</o:Version>
 </o:DocumentProperties>
</xml><![endif]--><!--[if gte mso 9]><xml>
 <w:WordDocument>
  <w:SpellingState>Clean</w:SpellingState>
  <w:GrammarState>Clean</w:GrammarState>
  <w:PunctuationKerning/>
  <w:ValidateAgainstSchemas/>
  <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
  <w:IgnoreMixedContent>false</w:IgnoreMixedContent>
  <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
  <w:Compatibility>
   <w:BreakWrappedTables/>
   <w:SnapToGridInCell/>
   <w:WrapTextWithPunct/>
   <w:UseAsianBreakRules/>
   <w:DontGrowAutofit/>
   <w:UseFELayout/>
  </w:Compatibility>
  <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
 </w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
 <w:LatentStyles DefLockedState="false" LatentStyleCount="156">
 </w:LatentStyles>
</xml><![endif]-->
<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:Wingdings;
	panose-1:5 0 0 0 0 0 0 0 0 0;
	mso-font-charset:2;
	mso-generic-font-family:auto;
	mso-font-pitch:variable;
	mso-font-signature:0 268435456 0 0 -2147483648 0;}
@font-face
	{font-family:宋体;
	panose-1:2 1 6 0 3 1 1 1 1 1;
	mso-font-alt:SimSun;
	mso-font-charset:134;
	mso-generic-font-family:auto;
	mso-font-pitch:variable;
	mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
	{font-family:"\@宋体";
	panose-1:2 1 6 0 3 1 1 1 1 1;
	mso-font-charset:134;
	mso-generic-font-family:auto;
	mso-font-pitch:variable;
	mso-font-signature:3 135135232 16 0 262145 0;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{mso-style-parent:"";
	margin:0cm;
	margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:12.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:宋体;}
h3
	{mso-style-next:Normal;
	margin-top:12.0pt;
	margin-right:0cm;
	margin-bottom:3.0pt;
	margin-left:0cm;
	mso-pagination:widow-orphan;
	page-break-after:avoid;
	mso-outline-level:3;
	font-size:13.0pt;
	font-family:Arial;}
span.SpellE
	{mso-style-name:"";
	mso-spl-e:yes;}
@page Section1
	{size:612.0pt 792.0pt;
	margin:72.0pt 90.0pt 72.0pt 90.0pt;
	mso-header-margin:35.4pt;
	mso-footer-margin:35.4pt;
	mso-paper-source:0;}
div.Section1
	{page:Section1;}
 /* List Definitions */
 @list l0
	{mso-list-id:167642836;
	mso-list-type:hybrid;
	mso-list-template-ids:769981794 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;}
@list l0:level1
	{mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:36.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;
	font-family:Symbol;}
ol
	{margin-bottom:0cm;}
ul
	{margin-bottom:0cm;}
-->
</style>
<!--[if gte mso 10]>
<style>
 /* Style Definitions */
 table.MsoNormalTable
	{mso-style-name:"Table Normal";
	mso-tstyle-rowband-size:0;
	mso-tstyle-colband-size:0;
	mso-style-noshow:yes;
	mso-style-parent:"";
	mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
	mso-para-margin:0cm;
	mso-para-margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:10.0pt;
	font-family:"Times New Roman";
	mso-ansi-language:#0400;
	mso-fareast-language:#0400;
	mso-bidi-language:#0400;}
</style>
<![endif]--><!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="2050"/>
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1"/>
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=ZH-CN style='tab-interval:36.0pt'>

<div class=Section1>

<h3><span lang=EN-US>99. </span><span style='font-family:宋体;mso-ascii-font-family:
Arial;mso-hansi-font-family:Arial'>不要使用无效的对象。不要使用不安全的函数。</span></h3>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span
style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>摘要</span><span lang=EN-US><o:p></o:p></span></b></p>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman"'>不要服用过期的药物:无效的对象和不安全的历史遗留函数会对程序的稳定性造成极大的破坏。</span></p>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span
style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>讨论</span><span lang=EN-US><o:p></o:p></span></b></p>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman"'>主要有三种无效对象:</span></p>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<ul style='margin-top:0cm' type=disc>
 <li class=MsoNormal style='mso-list:l0 level1 lfo1;tab-stops:list 36.0pt'><span
     style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
     "Times New Roman"'>已销毁的对象:典型的例子就是离开了作用域的自动对象和已删除的从堆中分配的对象。在调用对象的析构函数之后,它的生命期就此结束,继续用它进行任何操作其结果是未定义的,通常来说是不安全的。</span></li>
</ul>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<ul style='margin-top:0cm' type=disc>
 <li class=MsoNormal style='mso-list:l0 level1 lfo1;tab-stops:list 36.0pt'><span
     style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
     "Times New Roman"'>语义上无效的对象:典型的例子包括指向已删除对象的</span><span lang=EN-US>dangling</span><span
     style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
     "Times New Roman"'>指针(例如:在</span><span lang=EN-US>delete p; </span><span
     style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
     "Times New Roman"'>之后的指针</span><span lang=EN-US>p</span><span
     style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
     "Times New Roman"'>),已作废的迭代器(例如:在容器头部执行过插入操作后的</span><span lang=EN-US>vector&lt;T&gt;::<span
     class=SpellE>iterator</span> i</span><span style='font-family:宋体;
     mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>)。注意</span><span
     lang=EN-US>dangling</span><span style='font-family:宋体;mso-ascii-font-family:
     "Times New Roman";mso-hansi-font-family:"Times New Roman"'>指针和已作废的迭代器从概念上讲是完全一样的,而后一种形式通常直接包含了前一种。除了重新让它们指向有效的对象(例如:</span><span
     lang=EN-US>p = new Object; </span><span style='font-family:宋体;mso-ascii-font-family:
     "Times New Roman";mso-hansi-font-family:"Times New Roman"'>或</span><span
     lang=EN-US> I = <span class=SpellE>v.begin</span>(); </span><span
     style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
     "Times New Roman"'>)之外,继续用它们进行任何操作其结果都是未定义的,通常来说是不安全的。</span></li>
</ul>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<ul style='margin-top:0cm' type=disc>
 <li class=MsoNormal style='mso-list:l0 level1 lfo1;tab-stops:list 36.0pt'><span
     style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
     "Times New Roman"'>从来都无效的对象:这样的例子包括通过伪造的指针(用</span><span class=SpellE><span
     lang=EN-US>reinterpret_cast</span></span><span style='font-family:宋体;
     mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>,参见第</span><span
     lang=EN-US>92</span><span style='font-family:宋体;mso-ascii-font-family:
     "Times New Roman";mso-hansi-font-family:"Times New Roman"'>条)“得到”的对象,或越界访问数组。</span></li>
</ul>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman"'>要知道对象的生命期和有效性。绝对不要解引(</span><span
lang=EN-US>dereference</span><span style='font-family:宋体;mso-ascii-font-family:
"Times New Roman";mso-hansi-font-family:"Times New Roman"'>)无效的迭代器或指针。不要假设</span><span
lang=EN-US>delete</span><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman"'>会做什么和不做什么;释放了的内存已经被释放了,在任何情况下都不应该再使用它。不要设法摆弄对象的生命期:即通过手动地调用析构函数(例如:</span><span
class=SpellE><span lang=EN-US>obj.~T</span></span><span lang=EN-US>()</span><span
style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>)然后再调用</span><span lang=EN-US>placement new</span><span
style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>。(参见第</span><span lang=EN-US>55</span><span
style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>条)</span></p>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman"'>不要使用</span><span lang=EN-US>C</span><span
style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>遗留的不安全的函数:</span><span class=SpellE><span lang=EN-US>strcpy</span></span><span
style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>,</span><span class=SpellE><span lang=EN-US>strncpy</span></span><span
style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>,</span><span class=SpellE><span lang=EN-US>sprintf</span></span><span
style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>,或其它不检查写入缓冲区的长度,不进行越界检查,在越界时不能正确处理的函数。</span><span
lang=EN-US>C</span><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman"'>的</span><span class=SpellE><span
lang=EN-US>strcpy</span></span><span style='font-family:宋体;mso-ascii-font-family:
"Times New Roman";mso-hansi-font-family:"Times New Roman"'>不检查缓冲区的长度,</span><span
lang=EN-US>[C99]</span><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman"'>的</span><span class=SpellE><span
lang=EN-US>strncpy</span></span><span style='font-family:宋体;mso-ascii-font-family:
"Times New Roman";mso-hansi-font-family:"Times New Roman"'>虽然检查缓冲区的长度,但是在达到缓冲区的最大长度时不会添加</span><span
lang=EN-US>null</span><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman"'>,这两个函数都可能导致程序崩溃,都是安全隐患。请使用更新的,更安全的,更复杂的结构和函数,如</span><span
lang=EN-US>C++</span><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman"'>标准库中的那些(参见第</span><span lang=EN-US>77</span><span
style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>条)。虽然它们也并非百分之百的安全(为了效率),但是它们更不容易出错,可以更好地构筑安全的代码。</span></p>

<p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>

</div>

</body>

</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -