﻿<?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++博客-geforceX的编程小苑-随笔分类-算法/数据结构</title><link>http://www.cppblog.com/geforcex/category/674.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 23 May 2008 18:05:27 GMT</lastBuildDate><pubDate>Fri, 23 May 2008 18:05:27 GMT</pubDate><ttl>60</ttl><item><title>一个关于指针的问题</title><link>http://www.cppblog.com/geforcex/archive/2005/12/21/1936.html</link><dc:creator>geforceX</dc:creator><author>geforceX</author><pubDate>Wed, 21 Dec 2005 02:55:00 GMT</pubDate><guid>http://www.cppblog.com/geforcex/archive/2005/12/21/1936.html</guid><wfw:comment>http://www.cppblog.com/geforcex/comments/1936.html</wfw:comment><comments>http://www.cppblog.com/geforcex/archive/2005/12/21/1936.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/geforcex/comments/commentRss/1936.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/geforcex/services/trackbacks/1936.html</trackback:ping><description><![CDATA[有些东西在你没有遇到之前还真是不知道自己没有弄清楚，也许是我平时细节的地方注意少了，看来以后要多加注意了。<BR><BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pStr;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;ch;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;str[]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Hello</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>ch&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;str[</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">];<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pStr&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;str[</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">];<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">ch&nbsp;=&nbsp;%c\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;ch);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">*pStr&nbsp;=&nbsp;%c</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pStr);</SPAN></DIV><BR>此时打印ch值的时候能正确输出，但是打印pStr[0]的时候就出问题了，跟踪发现是pStr[0] = str[1];这句出现问题。<BR><BR>原来，char *pStr; 只定义了一个4字节的指针变量，而这个变量里面的内容是将要指向一个char类型变量的，但是此时pStr只是个“野指针”，并没有指向一个char类型的内存单元，所以，当用*pStr来访问这个元素时，系统根本不知道该访问何处的空间，因此，在使用pStr前，必须让它指向一个具体的空间。<BR><BR>由上面可以将相关语句改为 
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pStr;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;ch;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;str[]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Hello</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>pStr&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">ch;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">pStr指向ch、获得初始化</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">pStr&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">str[</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">];&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">pStr指向str[1]地址、获得初始化</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">ch&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;str[</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">];<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pStr&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;str[</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">];<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">ch&nbsp;=&nbsp;%c\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;ch);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">*pStr&nbsp;=&nbsp;%c</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pStr);</SPAN></DIV>也就是说，要先给指针一个地址值初始化它，然后才能用*访问它指向的内容。<BR><BR><BR><BR>◎另外，<FONT color=#ff0000>在子函数中使用malloc()/new()分配的内存空间不会因子函数的返回而消失</FONT>，函数只会清理调子函数里定义的变量的空间，如： 
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;func()<BR><IMG id=Codehighlighter1_13_77_Open_Image onclick="this.style.display='none'; Codehighlighter1_13_77_Open_Text.style.display='none'; Codehighlighter1_13_77_Closed_Image.style.display='inline'; Codehighlighter1_13_77_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_13_77_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_13_77_Closed_Text.style.display='none'; Codehighlighter1_13_77_Open_Image.style.display='inline'; Codehighlighter1_13_77_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_13_77_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_13_77_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">p;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;p&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)malloc(</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">));<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;p;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">返回后，p所在空间被释放，但是p所指空间还存在</SPAN></DIV>所以，可以用p1=func();来获得在func()中分配的空间。<BR><BR>此中方法可以用来解决“指针的指针”使用不方便的问题。步骤分2步：1、把传入的指针的指针参数去掉；2、把函数的返回值赋给要改变的对象的指针： 
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;GetMemory2(</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">**</SPAN><SPAN style="COLOR: #000000">p,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;num)<BR><IMG id=Codehighlighter1_35_80_Open_Image onclick="this.style.display='none'; Codehighlighter1_35_80_Open_Text.style.display='none'; Codehighlighter1_35_80_Closed_Image.style.display='inline'; Codehighlighter1_35_80_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_35_80_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_35_80_Closed_Text.style.display='none'; Codehighlighter1_35_80_Open_Image.style.display='inline'; Codehighlighter1_35_80_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_35_80_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_35_80_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&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: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)malloc(</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;num);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;Test2(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">)<BR><IMG id=Codehighlighter1_99_230_Open_Image onclick="this.style.display='none'; Codehighlighter1_99_230_Open_Text.style.display='none'; Codehighlighter1_99_230_Closed_Image.style.display='inline'; Codehighlighter1_99_230_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_99_230_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_99_230_Closed_Text.style.display='none'; Codehighlighter1_99_230_Open_Image.style.display='inline'; Codehighlighter1_99_230_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_99_230_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_99_230_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">str&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;NULL;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;GetMemory2(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">str,&nbsp;</SPAN><SPAN style="COLOR: #000000">100</SPAN><SPAN style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;注意参数是&nbsp;&amp;str，而不是str</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;strcpy(str,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">hello</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;cout</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;str&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;endl;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;free(str);&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_233_254_Open_Image onclick="this.style.display='none'; Codehighlighter1_233_254_Open_Text.style.display='none'; Codehighlighter1_233_254_Closed_Image.style.display='inline'; Codehighlighter1_233_254_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_233_254_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_233_254_Closed_Text.style.display='none'; Codehighlighter1_233_254_Open_Image.style.display='inline'; Codehighlighter1_233_254_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_233_254_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_233_254_Open_Text><SPAN style="COLOR: #808080">//////</SPAN><SPAN style="COLOR: #008000">下面是用传递动态内存的方法实现</SPAN><SPAN style="COLOR: #808080"></SPAN></SPAN><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">GetMemory3(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;num)<BR><IMG id=Codehighlighter1_281_343_Open_Image onclick="this.style.display='none'; Codehighlighter1_281_343_Open_Text.style.display='none'; Codehighlighter1_281_343_Closed_Image.style.display='inline'; Codehighlighter1_281_343_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_281_343_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_281_343_Closed_Text.style.display='none'; Codehighlighter1_281_343_Open_Image.style.display='inline'; Codehighlighter1_281_343_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_281_343_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_281_343_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&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: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)malloc(</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;num);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;p;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;Test3(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">)<BR><IMG id=Codehighlighter1_362_470_Open_Image onclick="this.style.display='none'; Codehighlighter1_362_470_Open_Text.style.display='none'; Codehighlighter1_362_470_Closed_Image.style.display='inline'; Codehighlighter1_362_470_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_362_470_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_362_470_Closed_Text.style.display='none'; Codehighlighter1_362_470_Open_Image.style.display='inline'; Codehighlighter1_362_470_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_362_470_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_362_470_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">str&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;NULL;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;str&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;GetMemory3(</SPAN><SPAN style="COLOR: #000000">100</SPAN><SPAN style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;strcpy(str,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">hello</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;cout</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;str&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;endl;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;free(str);&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV>因为此时用malloc/new分配的内存空间是堆里面的，函数返回时不会回收；而如果在子函数中用char p[]="asdfgewq";则p是在栈中定义的（先分配p的空间，然后讲字符串拷贝进去，跟char *p="asdfasdf"分配在全局静态内存中不一样），所以会被系统收回。<img src ="http://www.cppblog.com/geforcex/aggbug/1936.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/geforcex/" target="_blank">geforceX</a> 2005-12-21 10:55 <a href="http://www.cppblog.com/geforcex/archive/2005/12/21/1936.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数组和指针的比较</title><link>http://www.cppblog.com/geforcex/archive/2005/12/18/1863.html</link><dc:creator>geforceX</dc:creator><author>geforceX</author><pubDate>Sun, 18 Dec 2005 13:40:00 GMT</pubDate><guid>http://www.cppblog.com/geforcex/archive/2005/12/18/1863.html</guid><wfw:comment>http://www.cppblog.com/geforcex/comments/1863.html</wfw:comment><comments>http://www.cppblog.com/geforcex/archive/2005/12/18/1863.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/geforcex/comments/commentRss/1863.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/geforcex/services/trackbacks/1863.html</trackback:ping><description><![CDATA[1。定义数组变量时必须指定数组元素个数，因为系统会根据元素个数在编译时一次性分配这么多内存；<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 而指针变量只需要指定类型，系统只为该指针变量分配4字节（32位机）内存，而不会为该变量所指内容分配内存。<BR><BR>2。指针变量是有存储空间的；而数组名仅仅是一个标号，没有实际存储空间，单单一个数组名就只能表示该数组的第1个元素的地址。int a[10]; 规定&amp;a就等于&amp;a[0]或者a .<BR><BR>3。例如 char str[]= "hello world"和char *p = "hello world"中，数组str是先分配给他元素个数个内存，然后将后面的字符串复制给这个空间（注意，此时应该考虑\0字符）；而p的建立过程是先在静态存储区建立常字符串"hello world\0"，然后将p指向这个常字符串。所以数组str中元素的内容可以通过重新赋值改变，而p指向的内容不能改变。<img src ="http://www.cppblog.com/geforcex/aggbug/1863.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/geforcex/" target="_blank">geforceX</a> 2005-12-18 21:40 <a href="http://www.cppblog.com/geforcex/archive/2005/12/18/1863.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++里"const"关键字相关问题</title><link>http://www.cppblog.com/geforcex/archive/2005/12/10/1661.html</link><dc:creator>geforceX</dc:creator><author>geforceX</author><pubDate>Sat, 10 Dec 2005 12:08:00 GMT</pubDate><guid>http://www.cppblog.com/geforcex/archive/2005/12/10/1661.html</guid><wfw:comment>http://www.cppblog.com/geforcex/comments/1661.html</wfw:comment><comments>http://www.cppblog.com/geforcex/archive/2005/12/10/1661.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/geforcex/comments/commentRss/1661.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/geforcex/services/trackbacks/1661.html</trackback:ping><description><![CDATA[<P>转载的一篇文章：
<HR>
本篇主要写给对const语法理解存在误区的c++学习者，希望所有对这方面比较模糊的朋友可以阅读并从中找到一些答案。 <BR>。 <BR>2004-11-19 21:00 <BR>Const 最早想法是用于取代预处理器#define 这个宏，从而形成<A name=1></A>常量的概念。针对常量const对象，const指针及指向const的指针，函数const类型参数，const 函数返回类型， const类成员，及const成员函数，及对const最后理解的一些总结来描述 const。 <BR>① const对象和const类型的对象 <BR>对于这两个概念的描述如下 <BR>1. int const Object; //Object是一个const量是不可以被修改 Object = 2;Error <BR>2. const int Object; //Object是 const int型他所存放的内容不可以被修改 <BR>对于1，2这两种const用于对象，表述虽然不同但是效果是一样的。因为对象本身存放着内容对对象的改变就是对于对象内容的改变，同样改变后者也是在改变前者。所以语义上一样的。 <BR>② const指针 和 指向const的指针 及两者结合 <BR>对于三个概念描述如下 <BR>1. int* const p; //指针p是const不能被修改 例如p++; //修改p本身会Error <BR>//修改p指向内容 *p = 2; //OK <BR>2. const int* p; //p是指向一个整形常量的指针指向的内容不可以改变 p++;//OK <BR>// *p = 2; //Error <BR>3. const int* const p; //指针p本身是不能被修改并且p所有有效的内容也不能被 <BR>//修改 *p = 2; Error 和 p++; Error <BR>③ const 参数修饰 和 参数返回类型的const修饰 <BR>1.const 参数修饰 <BR>此时函数参数修饰 const的具体用法 ① ②中用法是一样的 <BR>例如 void Fun( const int I ) { I++;} //Error不能修改常量I <BR>2.const修饰函数返回类型用法也是类似于 ①②中,仅仅修饰的对象变化变成一个返回对象 <BR>例如：const int Fun() { static int I; return I;} <BR>int Res = (Fun())++ //Error不能修改常量返回对象 <BR>④ const类成员 和 const 成员函数 <BR>1. const成员 <BR>类const成员在构造期间会允许被初始化并且在以后不能被改变。我们就可以知道类const成员和一般const 变量是有所不同的，类const成员是对应于每个对象而言才有意义。因为他在构造期被初始化，只有当类实例化后才会进行构造。所以类const成员可以这样描述: 在类的每一次实例化时被初始化，在这个对象的生存周期中不可改变。 <BR>2. const 成员函数 <BR>描述: void Class::MemberFun() const {}; //此时这个const修饰的this所有类成员变量都不允许在这个函数体作用后被修改。这在设计上会带来一些好处，能防止你意外的处理带来的问题。 <BR>总结: <BR>&lt;1&gt; const 常量 一般编译器不会分配空间只是维护一张表。而当extern 外部引用这个常量或者“&amp;”对这个常量取地址时，编译器才会为其分配地址。Const本身的机制比较复杂。 <BR>&lt;2&gt; const 记忆法则 const修饰后面一个最近的名称。我曾初学的时候被const 修饰搞的糊里糊涂，后来慢慢的总结我觉得这样理解最容易的。 <BR>例子: const int I; 此时const仅仅修饰int 表明 I不是一个常量但是I的内容是常量。因为c/c++表达 对I的改变就是对I内容的改变所以 I也类似一个const。大家不妨可以用指针const修饰试试理解会有帮助的我想。 <BR>&lt;3&gt; 对于所有非const 类型可以无条件转化为 const类型，但是后者不能自动转化为前者除非显式的强制转化去掉const性。这样做是有意义的，因为const类型是非const的一个子集是一种特殊，由普遍转化为特殊是合理的，就象模板特化，继承的向上映射都是有意义的。 <BR>&lt;4&gt; 记住所有const修饰的内容并不是永远不可改变，如果人为的强制转化编译器是不会提醒的。因为它没有义务这么做，所以我们对其转化时要小心。 <BR>&lt;5&gt; 在const类成员函数处理时，我们引入了mutable修饰类成员变量，经过其修饰的成员变量可以在const类成员函数中被修改，编译器是允许的。而其他未被mutable修饰的成员还是按照const规则不能在const成员函数中被改变。
<HR>
 <BR><BR><BR>函数后面加“const”与不加是两个不同函数，如：</P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">#include&nbsp;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></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 src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;A<BR><IMG id=Codehighlighter1_41_135_Open_Image onclick="this.style.display='none'; Codehighlighter1_41_135_Open_Text.style.display='none'; Codehighlighter1_41_135_Closed_Image.style.display='inline'; Codehighlighter1_41_135_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_41_135_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_41_135_Closed_Text.style.display='none'; Codehighlighter1_41_135_Open_Image.style.display='inline'; Codehighlighter1_41_135_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_41_135_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_41_135_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR><IMG id=Codehighlighter1_66_80_Open_Image onclick="this.style.display='none'; Codehighlighter1_66_80_Open_Text.style.display='none'; Codehighlighter1_66_80_Closed_Image.style.display='inline'; Codehighlighter1_66_80_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_66_80_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_66_80_Closed_Text.style.display='none'; Codehighlighter1_66_80_Open_Image.style.display='inline'; Codehighlighter1_66_80_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>A(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;j)</SPAN><SPAN id=Codehighlighter1_66_80_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_66_80_Open_Text><SPAN style="COLOR: #000000">{a&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;i;&nbsp;b&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;j;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;print();<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;print()&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">:<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;a,&nbsp;b;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;A::print()<BR><IMG id=Codehighlighter1_155_193_Open_Image onclick="this.style.display='none'; Codehighlighter1_155_193_Open_Text.style.display='none'; Codehighlighter1_155_193_Closed_Image.style.display='inline'; Codehighlighter1_155_193_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_155_193_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_155_193_Closed_Text.style.display='none'; Codehighlighter1_155_193_Open_Image.style.display='inline'; Codehighlighter1_155_193_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_155_193_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_155_193_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>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">hello!</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;a&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;b&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;endl;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;A::print()&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_217_261_Open_Image onclick="this.style.display='none'; Codehighlighter1_217_261_Open_Text.style.display='none'; Codehighlighter1_217_261_Closed_Image.style.display='inline'; Codehighlighter1_217_261_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_217_261_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_217_261_Closed_Text.style.display='none'; Codehighlighter1_217_261_Open_Image.style.display='inline'; Codehighlighter1_217_261_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_217_261_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_217_261_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>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">hello&nbsp;const!</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;a&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;b&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;endl;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR><IMG id=Codehighlighter1_275_388_Open_Image onclick="this.style.display='none'; Codehighlighter1_275_388_Open_Text.style.display='none'; Codehighlighter1_275_388_Closed_Image.style.display='inline'; Codehighlighter1_275_388_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_275_388_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_275_388_Closed_Text.style.display='none'; Codehighlighter1_275_388_Open_Image.style.display='inline'; Codehighlighter1_275_388_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_275_388_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_275_388_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>A&nbsp;ss(</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">6</SPAN><SPAN style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;定义A的对象时，没有用“const”</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">ss.print();<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>A&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;dd(</SPAN><SPAN style="COLOR: #000000">20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;定义A的对象时，用了“const”</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">dd.print();<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV>
<P></P><img src ="http://www.cppblog.com/geforcex/aggbug/1661.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/geforcex/" target="_blank">geforceX</a> 2005-12-10 20:08 <a href="http://www.cppblog.com/geforcex/archive/2005/12/10/1661.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原码、反码、补码</title><link>http://www.cppblog.com/geforcex/archive/2005/12/09/1650.html</link><dc:creator>geforceX</dc:creator><author>geforceX</author><pubDate>Fri, 09 Dec 2005 11:32:00 GMT</pubDate><guid>http://www.cppblog.com/geforcex/archive/2005/12/09/1650.html</guid><wfw:comment>http://www.cppblog.com/geforcex/comments/1650.html</wfw:comment><comments>http://www.cppblog.com/geforcex/archive/2005/12/09/1650.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/geforcex/comments/commentRss/1650.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/geforcex/services/trackbacks/1650.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">&nbsp; 昨天去参加笔试，遇到了一题考计算机里存放的数据都是以原码、反码、还是补码的形式存放的。我还真的很久没有考虑过原码补码的问题了，不过凭他考这个问题，我可以猜出答案一定是补码了。虽然答案知道了，但是还真不知道还有这么一说，所以回来在网上搜了一篇这样的文章，才算是弄清楚是怎么一回事了：<BR><BR><BR>&nbsp;&nbsp;&nbsp; 数值有正负之分</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>(0</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为正</SPAN><SPAN lang=EN-US>,1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为负</SPAN><SPAN lang=EN-US>).</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这就是机器数的原码了</SPAN><SPAN lang=EN-US>.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">假设机器能处理的位数为</SPAN><SPAN lang=EN-US>8.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">即字长为</SPAN><SPAN lang=EN-US>1byte,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">原码能表示数值的范围为</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 15.75pt; TEXT-ALIGN: center; mso-char-indent-count: 1.5" align=center><SPAN lang=EN-US>(-127~-0 +0~127)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">共</SPAN><SPAN lang=EN-US>256</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个</SPAN><SPAN lang=EN-US>.</SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">有了数值的表示方法就可以对数进行算术运算</SPAN><SPAN lang=EN-US>.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">但是很快就发现用带符号位的原码进行乘除运算时结果正确</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">而在加减运算的时候就出现了问题</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如下</SPAN><SPAN lang=EN-US>: </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">假设字长为</SPAN><SPAN lang=EN-US>8bits<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt"><SPAN lang=EN-US>( 1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>10</SUB></SPAN>-<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( 1 )<SUB>10<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN></SUB><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>=<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( 1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>10</SUB>&nbsp;</SPAN>+ ( -1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>10</SUB>&nbsp;</SPAN>= <SPAN style="mso-spacerun: yes">&nbsp;</SPAN>( 0 )<SUB>10<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN></SUB></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt"><SPAN lang=EN-US>(00000001)<SUB>原</SUB><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>+ (10000001)<SUB>原<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN></SUB><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>= (10000010)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>原</SUB>&nbsp;</SPAN>= ( -2 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">显然不正确</SPAN><SPAN lang=EN-US>.</SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">因为在两个整数的加法运算中是没有问题的</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">于是就发现问题出现在带符号位的负数身上</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对除符号位外的其余各位逐位取反就产生了反码</SPAN><SPAN lang=EN-US>.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">反码的取值空间和原码相同且一一对应</SPAN><SPAN lang=EN-US>. </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下面是反码的减法运算</SPAN><SPAN lang=EN-US>:</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>( 1 )<SUB>10<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN></SUB><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>-<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( 1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>10</SUB></SPAN>=<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( 1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>10</SUB></SPAN>+ ( -1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>10</SUB></SPAN>= <SPAN style="mso-spacerun: yes">&nbsp;</SPAN>( 0 )<SUB>10<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN></SUB></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>(00000001)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>反</SUB></SPAN>+ (11111110)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>反</SUB>&nbsp;</SPAN>=<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(11111111)<SUB>反<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN></SUB><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>=<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( -0 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shape id=_x0000_i1057 style="WIDTH: 9.75pt; HEIGHT: 18pt" type="#_x0000_t75" o:ole=""> <v:imagedata src="file:///C:\WINDOWS\TEMP\msohtml1\01\clip_image005.wmz" o:title=""></v:imagedata></v:shape></SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">有问题</SPAN><SPAN lang=EN-US>.</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt"><SPAN lang=EN-US>( 1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"><SUB>10</SUB></SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>-<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( 2)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>10</SUB>&nbsp;</SPAN>=<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( 1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>10</SUB>&nbsp;</SPAN>+ ( -2 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>10</SUB>&nbsp;</SPAN>= <SPAN style="mso-spacerun: yes">&nbsp;</SPAN>( -1 )<SUB>10<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN></SUB></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt"><SPAN lang=EN-US>(00000001)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>反</SUB></SPAN>+ (11111101)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>反</SUB>&nbsp;</SPAN>=<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(11111110)<SUB>反<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN></SUB><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>=<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( -1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">正确</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">问题出现在</SPAN><SPAN lang=EN-US>(+0)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>(-0)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">上</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在人们的计算概念中零是没有正负之分的</SPAN><SPAN lang=EN-US>.(</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">印度人首先将零作为标记并放入运算之中</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">包含有零号的印度数学和十进制计数对人类文明的贡献极大</SPAN><SPAN lang=EN-US>).</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">于是就引入了补码概念</SPAN><SPAN lang=EN-US>. </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">负数的补码就是对反码加一</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">而正数不变</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">正数的原码反码补码是一样的</SPAN><SPAN lang=EN-US>.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在补码中用</SPAN><SPAN lang=EN-US>(-128)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">代替了</SPAN><SPAN lang=EN-US>(-0),</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所以补码的表示范围为</SPAN><SPAN lang=EN-US>:</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 16.5pt; TEXT-ALIGN: center; mso-char-indent-count: 1.57" align=center><SPAN lang=EN-US>(-128~0~127)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">共</SPAN><SPAN lang=EN-US>256</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个</SPAN><SPAN lang=EN-US>.</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 15.75pt; mso-char-indent-count: 1.5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">注意</SPAN><SPAN lang=EN-US>:(-128)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">没有相对应的原码和反码</SPAN><SPAN lang=EN-US>, (-128) = (10000000)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"><v:shape id=_x0000_i1072 style="WIDTH: 11.25pt; HEIGHT: 18pt" type="#_x0000_t75" o:ole=""> <v:imagedata src="file:///C:\WINDOWS\TEMP\msohtml1\01\clip_image011.wmz" o:title=""></v:imagedata></v:shape></SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">补码的加减运算如下</SPAN><SPAN lang=EN-US>:</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN lang=EN-US>( 1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>10</SUB></SPAN>-<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( 1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>10</SUB></SPAN>=<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( 1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>10</SUB>&nbsp;</SPAN>+ ( -1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>10</SUB>&nbsp;</SPAN>= <SPAN style="mso-spacerun: yes">&nbsp;</SPAN>( 0 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SUB>10</SUB></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt"><SPAN lang=EN-US>(00000001)<SUB>补</SUB><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>+ (11111111)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>补</SUB>&nbsp;</SPAN>=<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(00000000)<SUB>补<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN></SUB><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>= ( 0 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">正确</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt"><SPAN lang=EN-US>( 1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>10</SUB></SPAN>-<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( 2)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>10</SUB></SPAN>=<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>( 1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>10</SUB>&nbsp;</SPAN>+ ( -2 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>10</SUB>&nbsp;</SPAN>= <SPAN style="mso-spacerun: yes">&nbsp;</SPAN>( -1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SUB>10</SUB></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt"><SPAN lang=EN-US>(00000001)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>补</SUB></SPAN>+ (11111110)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes">&nbsp;<SUB>补</SUB></SPAN>=<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(11111111)<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"></SPAN><SPAN style="mso-spacerun: yes"><SUB>补</SUB>&nbsp;</SPAN>= ( -1 )<SPAN style="POSITION: relative; TOP: 6pt; mso-text-raise: -6.0pt"><v:shape id=_x0000_i1048 style="WIDTH: 9.75pt; HEIGHT: 18pt" type="#_x0000_t75" o:ole=""> <v:imagedata src="file:///C:\WINDOWS\TEMP\msohtml1\01\clip_image005.wmz" o:title=""></v:imagedata></v:shape></SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN></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><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所以补码的设计目的是</SPAN><SPAN lang=EN-US>:</SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; FONT-VARIANT: small-caps; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">⑴使符号位能与有效值部分一起参加运算</SPAN><SPAN lang=EN-US style="FONT-VARIANT: small-caps">,</SPAN><SPAN style="FONT-FAMILY: 宋体; FONT-VARIANT: small-caps; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">从而简化运算规则</SPAN><SPAN lang=EN-US style="FONT-VARIANT: small-caps">.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 26.25pt; mso-char-indent-count: 2.5"><SPAN style="FONT-FAMILY: 宋体; FONT-VARIANT: small-caps; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">⑵使减法运算转换为加法运算</SPAN><SPAN lang=EN-US style="FONT-VARIANT: small-caps">,</SPAN><SPAN style="FONT-FAMILY: 宋体; FONT-VARIANT: small-caps; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进一步简化计算机中运算器的线路设计</SPAN><SPAN lang=EN-US><o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所有这些转换都是在计算机的最底层进行的，而在我们使用的汇编、</SPAN><SPAN lang=EN-US>C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">等其他高级语言中使用的都是原码。看了上面这些大家应该对原码、反码、补码有了新的认识了吧！<BR><BR><BR>下面总结一下：<BR>1。正数的原码反码补码都相同，负数的反码是除符号位为1外，其他位全取反；补码就是反码+1<BR>2。(10000000)<SUB>补 </SUB>规定为-128<BR>3。计算机中的数据是以补码形式存储的</SPAN></P><img src ="http://www.cppblog.com/geforcex/aggbug/1650.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/geforcex/" target="_blank">geforceX</a> 2005-12-09 19:32 <a href="http://www.cppblog.com/geforcex/archive/2005/12/09/1650.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>终于弄清了几个长期模模糊糊的问题</title><link>http://www.cppblog.com/geforcex/archive/2005/11/26/1336.html</link><dc:creator>geforceX</dc:creator><author>geforceX</author><pubDate>Sat, 26 Nov 2005 02:33:00 GMT</pubDate><guid>http://www.cppblog.com/geforcex/archive/2005/11/26/1336.html</guid><wfw:comment>http://www.cppblog.com/geforcex/comments/1336.html</wfw:comment><comments>http://www.cppblog.com/geforcex/archive/2005/11/26/1336.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/geforcex/comments/commentRss/1336.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/geforcex/services/trackbacks/1336.html</trackback:ping><description><![CDATA[int型在16位、8位机上默认的是short int型，占2个字节，所以51单片机开发上的int类型都是2字节<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在32位机上默认的是long int型，占4个字节，所以一般PC上int类型都是4字节<BR><BR>指针类型所占空间不会因为所指变量类型的不同而不同，在16位8位机上都是2字节；在32位机上都是4字节，如char *a、int *b， sizeof(a)、sizeof(b)全部都等于4<BR><BR>signed char 类型的取值范围是 -128~127<img src ="http://www.cppblog.com/geforcex/aggbug/1336.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/geforcex/" target="_blank">geforceX</a> 2005-11-26 10:33 <a href="http://www.cppblog.com/geforcex/archive/2005/11/26/1336.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>