﻿<?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/sandy/category/767.html</link><description>高性能服务器端计算</description><language>zh-cn</language><lastBuildDate>Mon, 04 Jun 2012 01:32:26 GMT</lastBuildDate><pubDate>Mon, 04 Jun 2012 01:32:26 GMT</pubDate><ttl>60</ttl><item><title>逆向三国群英2</title><link>http://www.cppblog.com/sandy/archive/2012/05/31/176971.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Thu, 31 May 2012 13:53:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2012/05/31/176971.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/176971.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2012/05/31/176971.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/176971.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/176971.html</trackback:ping><description><![CDATA[<div><div style="layout-grid:15.6000pt; "><p style="margin-bottom:0pt; margin-top:0pt; "><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">闲来无事，搞搞逆向工程分析一下三国群英</span><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">2，今天的目标是去掉光盘提示,并让游戏正常进行。因为我玩的是原版，所以每当直接运行sango2.exe会弹出如下对话框:</span></p><p style="margin-bottom: 0pt; margin-top: 0pt;"><img src="file:///C:/DOCUME%7E1/qiqi/LOCALS%7E1/Temp/ksohtml/wps_clip_image-28588.png" alt="" height="3" width="2" /></p><p style="margin-bottom:0pt; margin-top:0pt; "><img src="http://www.cppblog.com/images/cppblog_com/sandy/s1.png" alt="" border="0" height="112" width="243" /><br /></p><p style="margin-bottom: 0pt; margin-top: 0pt;"><span style="font-size: 10.5pt; font-family: '宋体';"><br /></span></p><p style="margin-bottom:0pt; margin-top:0pt; "><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">我使用的工具是大名鼎鼎的OllyDBG。</span></p><p style="margin-bottom:0pt; margin-top:0pt; "><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">首先，打开OD,打开sango2.exe,&nbsp;运行程序，当出现对话框时暂时程序，发现停在此处：</span></p><p style="margin-bottom: 0pt; margin-top: 0pt;"><img src="http://www.cppblog.com/images/cppblog_com/sandy/s2.png" alt="" border="0" height="162" width="554" /></p><p style="margin-bottom:0pt; margin-top:0pt; "><br /></p><p style="margin-bottom:0pt; margin-top:0pt; "><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">仔细看以下几行代码，可以看出在40B207处有一个判断，如果EAX=0，则不显示对话框，并跳转到40B21F，那如果我们把B207的&#8220;JE&nbsp;SHORT&nbsp;0040B21F&#8221;改为&#8220;JNE&nbsp;SHORT&nbsp;0040B21F&#8221;,是否就可以不显示对话框呢？经测试，确实不显示了，但是游戏也直接退出了，并没有达到我们要的效果。看来这个地方只是出错后的程序处理，我们要找到更深的源头，才能解决这个问题。</span></p><p style="margin-bottom:0pt; margin-top:0pt; "><img src="http://www.cppblog.com/images/cppblog_com/sandy/s3.png" alt="" border="0" height="147" width="526" /><br /></p><p style="margin-bottom: 0pt; margin-top: 0pt;"><span style="font-size: 10.5pt; font-family: '宋体';"><br /></span></p><p style="margin-bottom:0pt; margin-top:0pt; "><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">上断点！我们要结合单步调试来理解程序的运行逻辑才能发现真正的解决方案。</span></p><p style="margin-bottom:0pt; margin-top:0pt; "><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">观察对话框出现的时候，程序的堆栈如下：</span></p><p style="margin-bottom:0pt; margin-top:0pt; "><img src="http://www.cppblog.com/images/cppblog_com/sandy/s4.png" alt="" border="0" height="151" width="554" /><br /></p><p style="margin-bottom: 0pt; margin-top: 0pt;"><span style="font-size: 10.5pt; font-family: '宋体';"><br /></span></p><p style="margin-bottom:0pt; margin-top:0pt; "><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">发现这个函数的入口在</span><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">40B00A，我们在此设置断点开始单步调试，会发现在调用4300F0出现对话框，跟踪进去。</span></p><p style="margin-bottom:0pt; margin-top:0pt; "><img src="http://www.cppblog.com/images/cppblog_com/sandy/s5.png" alt="" border="0" height="168" width="489" /><br /></p><p style="margin-bottom:0pt; margin-top:0pt; "><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">用这样的方法跟踪进去，保持耐心和清醒的头脑，最终会发现</span><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">4302C8会进行错误处理，导致对话框的出现，只要跳过即可。我们修改4302C1的代码为"JZ&nbsp;short&nbsp;004302D1"，即可跳过对话框，直接进行游戏。</span></p><p style="margin-bottom:0pt; margin-top:0pt; "><img src="http://www.cppblog.com/images/cppblog_com/sandy/s6.png" alt="" border="0" height="174" width="500" /><br /></p><p style="margin-bottom: 0pt; margin-top: 0pt;"><span style="font-size: 10.5pt; font-family: '宋体';"><br /></span></p><p style="margin-bottom:0pt; margin-top:0pt; "><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">最后一步，出补丁。使用右键功能【</span><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">copy&nbsp;to&nbsp;executable】，然后另存为可执行文件就可以了。运行修改过后的sango2.exe，就会发现已经破解成功了。</span></p><p style="margin-bottom:0pt; margin-top:0pt; "><img src="http://www.cppblog.com/images/cppblog_com/sandy/s7.png" alt="" border="0" height="177" width="554" /><br /></p></div></div><img src ="http://www.cppblog.com/sandy/aggbug/176971.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2012-05-31 21:53 <a href="http://www.cppblog.com/sandy/archive/2012/05/31/176971.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>debug to fix crash</title><link>http://www.cppblog.com/sandy/archive/2012/04/27/172918.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Fri, 27 Apr 2012 07:34:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2012/04/27/172918.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/172918.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2012/04/27/172918.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/172918.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/172918.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 最近遇到一个Windows Office Communicator 2007 崩溃的问题，有些意思，写下来跟大家分享。【现象】我们公司内部使用office communicator来做内部人员的IM工具，使用的是一个定制版本(plugin), 可以跟公司内部的组织架构做整合。我使用的OS是Windows 7 32bit,一开始使用并无问题，在某次windows update之后，发现没法添加好友，...&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2012/04/27/172918.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/172918.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2012-04-27 15:34 <a href="http://www.cppblog.com/sandy/archive/2012/04/27/172918.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>那些leveldb使用的奇技淫巧1-内存管理</title><link>http://www.cppblog.com/sandy/archive/2012/03/28/leveldb-trick1.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 28 Mar 2012 10:00:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2012/03/28/leveldb-trick1.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/169303.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2012/03/28/leveldb-trick1.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/169303.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/169303.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: leveldb中内存管理的技巧&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2012/03/28/leveldb-trick1.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/169303.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2012-03-28 18:00 <a href="http://www.cppblog.com/sandy/archive/2012/03/28/leveldb-trick1.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>奇怪的g++的行为</title><link>http://www.cppblog.com/sandy/archive/2008/08/13/58733.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 13 Aug 2008 08:56:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2008/08/13/58733.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/58733.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2008/08/13/58733.html#Feedback</comments><slash:comments>18</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/58733.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/58733.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 奇怪的g++的行为&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2008/08/13/58733.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/58733.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2008-08-13 16:56 <a href="http://www.cppblog.com/sandy/archive/2008/08/13/58733.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>printf的wrapper</title><link>http://www.cppblog.com/sandy/archive/2007/10/12/34022.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Fri, 12 Oct 2007 06:13:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2007/10/12/34022.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/34022.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2007/10/12/34022.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/34022.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/34022.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 如何写printf的wrapper函数&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2007/10/12/34022.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/34022.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2007-10-12 14:13 <a href="http://www.cppblog.com/sandy/archive/2007/10/12/34022.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何写出专业的C头文件</title><link>http://www.cppblog.com/sandy/archive/2007/05/15/24151.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Tue, 15 May 2007 05:38:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2007/05/15/24151.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/24151.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2007/05/15/24151.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/24151.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/24151.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 从lua源码中学到的一点小东西&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2007/05/15/24151.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/24151.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2007-05-15 13:38 <a href="http://www.cppblog.com/sandy/archive/2007/05/15/24151.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++源文件编码问题</title><link>http://www.cppblog.com/sandy/archive/2007/04/05/21320.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Thu, 05 Apr 2007 07:30:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2007/04/05/21320.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/21320.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2007/04/05/21320.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/21320.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/21320.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: C++源文件编码问题&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2007/04/05/21320.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/21320.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2007-04-05 15:30 <a href="http://www.cppblog.com/sandy/archive/2007/04/05/21320.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>{Just for fun} 如何让指针指向自己</title><link>http://www.cppblog.com/sandy/archive/2007/04/04/21229.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 04 Apr 2007 04:16:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2007/04/04/21229.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/21229.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2007/04/04/21229.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/21229.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/21229.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 如何让指针指向自己&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2007/04/04/21229.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/21229.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2007-04-04 12:16 <a href="http://www.cppblog.com/sandy/archive/2007/04/04/21229.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[STL] 循环中erase</title><link>http://www.cppblog.com/sandy/archive/2006/12/27/16913.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 27 Dec 2006 10:01:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/12/27/16913.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/16913.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/12/27/16913.html#Feedback</comments><slash:comments>11</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/16913.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/16913.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: [STL] loop & erase &nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2006/12/27/16913.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/16913.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-12-27 18:01 <a href="http://www.cppblog.com/sandy/archive/2006/12/27/16913.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>谈谈snprintf</title><link>http://www.cppblog.com/sandy/archive/2006/09/29/13133.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Fri, 29 Sep 2006 02:27:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/09/29/13133.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/13133.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/09/29/13133.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/13133.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/13133.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: snprintf的正确用法和错误用法&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2006/09/29/13133.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/13133.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-09-29 10:27 <a href="http://www.cppblog.com/sandy/archive/2006/09/29/13133.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>STL 备忘录</title><link>http://www.cppblog.com/sandy/archive/2006/07/12/9721.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 12 Jul 2006 03:19:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/07/12/9721.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/9721.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/07/12/9721.html#Feedback</comments><slash:comments>14</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/9721.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/9721.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 使用标准库应该记住的一些东西(21条)&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2006/07/12/9721.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/9721.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-07-12 11:19 <a href="http://www.cppblog.com/sandy/archive/2006/07/12/9721.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>G11N Developement (2) --- Linux Best Practice</title><link>http://www.cppblog.com/sandy/archive/2006/06/26/9020.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Mon, 26 Jun 2006 08:57:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/06/26/9020.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/9020.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/06/26/9020.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/9020.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/9020.html</trackback:ping><description><![CDATA[在linux/Unix平台上面做G11N的开发，大抵都会用到gettext库/工具集( <a href="ftp://ftp.gnu.org/gnu/gettext/" target="_top">ftp://ftp.gnu.org/gnu/gettext/</a>&nbsp;) 和libiconv(<a href="http://www.gnu.org/software/libiconv/">http://www.gnu.org/software/libiconv/</a>) ,前者是用于实现Resource bundle,而后者用于各种编码转化。<br><br>(注:这里没有考虑cross-platform)<br><br><br>1. 实现方法<br><br>a . 使用po文件作为资源文件 (建议编码是utf-8)<br>&nbsp;&nbsp;&nbsp; 一方面因为UTF-8兼容ANSI,另外一方面考虑到G11N的程序大部分使用utf-8输出，比如输出到database,web UI, file等等。<br><br>b. 使用getext从resource file ( mo文件)读到相应的L10N resource文件<br>&nbsp;&nbsp;&nbsp;注意这里需要先setlocale来设置locale<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: #eeeeee;"><span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">locale.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">libintl.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br><br></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;_(string)&nbsp;gettext&nbsp;(string)</span><span style="color: #000000;"><br><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;argc,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">**</span><span style="color: #000000;">argv)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(setlocale(LC_ALL,&nbsp;&#8220;&#8221;)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;NULL)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&#8220;setlocale()&nbsp;error.\n&#8221;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>}<br></span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(bindtextdomain(package,&nbsp;&#8220;</span><span style="color: #000000;">/</span><span style="color: #000000;">usr</span><span style="color: #000000;">/</span><span style="color: #000000;">share</span><span style="color: #000000;">/</span><span style="color: #000000;">locale&#8221;)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;NULL)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&#8220;bindtextdomain()&nbsp;error.\n&#8221;);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;&#8211;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(textdomain(package)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;NULL)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&#8220;textdomain()&nbsp;error.\n&#8221;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;&#8211;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>}<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;</span><span style="color: #000000;">%</span><span style="color: #000000;">s\n&#8221;,&nbsp;_(&#8220;Hello,&nbsp;world</span><span style="color: #000000;">!</span><span style="color: #000000;">&#8221;));<br>&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>}<br></span></div>
<br><br>c.如果要输出到控制台(console),因为不是console都支持unicode output，所以推荐的方法是首先是把utf-8转化为本地编码，然后使用printf输出。不推荐使用wprintf进行输出，一方面是因为wchar_t的大小随编译器不同，不好控制。另外一方面，很多wprintf的实现也都是先把wchar_t[]转为本地编码，然后输出。转化编码使用libconv<br><br>d.如果要输出到web pages,database,file，编码推荐使用utf-8.<br><br>e.如果要输出本地化的日期和时间，使用API: strftime<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: #eeeeee;"><span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">time.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;argc,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">**</span><span style="color: #000000;">argv)&nbsp;<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;time_t&nbsp;t;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;tm&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ptm;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;buffer[</span><span style="color: #000000;">100</span><span style="color: #000000;">];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;memset(buffer,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(buffer));<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(time(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">t)&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&#8220;time()&nbsp;error:&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">m\n&#8221;);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;((ptm&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;localtime(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">t))&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;NULL)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&#8220;localtime()&nbsp;error:&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">m\n&#8221;);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;strftime(buffer,&nbsp;</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(buffer),&nbsp;&#8220;</span><span style="color: #000000;">%</span><span style="color: #000000;">x&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">X&#8221;,&nbsp;ptm);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;</span><span style="color: #000000;">%</span><span style="color: #000000;">s\n&#8221;,&nbsp;buffer);<br>&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>}<br></span></div>
<br><br>f. 如果要输出本地化的数字和货币，使用API: strfmon<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: #eeeeee;"><span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">monetary.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;argc,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">**</span><span style="color: #000000;">argv)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;buffer[</span><span style="color: #000000;">100</span><span style="color: #000000;">];<br>&nbsp;&nbsp;&nbsp;&nbsp;strfmon(buffer,&nbsp;</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(buffer),&nbsp;&#8220;</span><span style="color: #000000;">%=*</span><span style="color: #000000;">i</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;12345.67);</span><span style="color: #000000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;</span><span style="color: #000000;">%</span><span style="color: #000000;">s\n&#8221;,&nbsp;buffer);<br>&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>}<br></span></div>
<br><br>2.目录结构<br>/product<br>&nbsp;&nbsp;/i18n<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/zh_TW<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;your.mo<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/ja_JP&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;your.mo<br><br><img src ="http://www.cppblog.com/sandy/aggbug/9020.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-06-26 16:57 <a href="http://www.cppblog.com/sandy/archive/2006/06/26/9020.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入 printf / wprintf / console下的unicode output</title><link>http://www.cppblog.com/sandy/archive/2006/06/21/8779.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 21 Jun 2006 03:53:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/06/21/8779.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/8779.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/06/21/8779.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/8779.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/8779.html</trackback:ping><description><![CDATA[1. printf 只能提供ANSI/MB 的输出，不支持输出unicode stream.<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"><span style="COLOR: #000000">wchar_t test[]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试1234</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br />printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,test);</span></div>是不会正确输出的<br /><br /><br />2.wprintf 同样不会提供unicode output,<br />   但是他会把wchar_t的string转为locale的SB/MB字符编码，然后输出<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"><span style="COLOR: #000000">wchar_t test[] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试Test</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br />wprintf(L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,test);</span></div>会输出??1234之类的字符串，或者不输出任何结果<br />因为wprintf没有办法把L"测试Test"转为默认的ANSI,需要设置locale<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"><span style="COLOR: #000000">setlocale(LC_ALL,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">chs</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />wchar_t test[] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试Test</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br />wprintf(L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,test);</span></div>会有正确的输出<br />等同于printf("%ls",test);<br /><br />综上:  <font color="#ff3300"><strong>CRT I/O functions do not provide Unicode output.<br /><br /></strong><font color="#000000">3. Window console自从NT4就是一个真正的unicode console<br />不过输出unicode string,只有使用Windows API, WriteConsoleW<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"><span style="COLOR: #000000">wchar_t test[] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试1234</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br />DWORD ws;<br />WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),test,wcslen(test),</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ws,NULL);</span></div>可以正确的输出而不需要设置locale,因为是真正的unicode的输出，跟codepage无关<br /><br />4. 如何实现跨平台的console output<br />    不要使用wchar_t和wprintf,因为这些都依赖于编译器.<br />     ICU是IBM的一个成熟的跨平台支持unicode的libary,推荐使用<br /><br />以下是ICU的uprintf实现<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"><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> uprintf(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> UnicodeString </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">str) {<br />    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">buf </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br />    int32_t len </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> str.length();<br />    int32_t bufLen </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> len </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">16</span><span style="COLOR: #000000">;<br />    int32_t actualLen;<br />    buf </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">[bufLen </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br />    actualLen </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> str.extract(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, len, buf</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">, bufLen</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000">); </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Default codepage conversion</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    buf[actualLen] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br />    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, buf);<br />    delete buf;<br />}</span></div>它也是先把Unicode string转化为本地的codepage,然后printf，虽然也不是unicode output,但是跨平台，大多数情况会工作得很好。<br /></font></font><img src ="http://www.cppblog.com/sandy/aggbug/8779.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-06-21 11:53 <a href="http://www.cppblog.com/sandy/archive/2006/06/21/8779.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>G11N Development(1) --- Unicode Basic &amp;&amp; wxxx function</title><link>http://www.cppblog.com/sandy/archive/2006/06/19/8722.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Mon, 19 Jun 2006 11:45:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/06/19/8722.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/8722.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/06/19/8722.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/8722.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/8722.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Globalization development &nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2006/06/19/8722.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/8722.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-06-19 19:45 <a href="http://www.cppblog.com/sandy/archive/2006/06/19/8722.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>漫谈函数的返回值</title><link>http://www.cppblog.com/sandy/archive/2006/04/25/6252.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Tue, 25 Apr 2006 08:55:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/04/25/6252.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/6252.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/04/25/6252.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/6252.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/6252.html</trackback:ping><description><![CDATA[在windows的世界里面，很少有API没有返回值。但是到底返回什么代表成功，这个没有标准。我发现主要有三种模式<br /><br /><strong>1 . 返回非0表示成功，返回0表示失败</strong><br /><br />大多数Win32 Platform API都是这样,比如<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"><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> result </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">MoveFileEx(szTempName, <br />                    </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">allcaps.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, <br />                    MOVEFILE_REPLACE_EXISTING);<br /><br /></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">result)<br />    { <br />        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Could not move file. error:%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,GetLastError());<br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br />    }<br /></span></div><br />使用这种方法。你必须提供类似GetLastError的取错误的方法，而且你必须保证这个函数是thread-safe的，每个线程能维护自己的错误信息。<br /><br /><strong>2. 返回大于等于0表示成功，返回-1表示失败<br /></strong>socket api大部分是这样设计的<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"><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">( bytesRecv </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> SOCKET_ERROR ) {<br />    bytesRecv </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> recv( ConnectSocket, recvbuf, </span><span style="COLOR: #000000">32</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000"> );<br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> ( bytesRecv </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">||</span><span style="COLOR: #000000"> bytesRecv </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> WSAECONNRESET ) {<br />      printf( </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Connection Closed.\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />      </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br />    }<br />    printf( </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Bytes Recv: %ld\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, bytesRecv );<br />  }<br /></span></div><br />这样的好处是返回值就可以用来表示成功和状态。比如这里的recv就可以返回收到的字节数。但是你还是要有一个查询错误的API,like WSAGetLastError().<br /><br /><strong>3.返回0表示成功<br /></strong><br />COM的接口大部分是这样设计的<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"><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">( FAILED(lpdd</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">QueryInterface(IID_IDirectDraw7, (LPVOID </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">lpdd)))<br />    {<br />          </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">error handle and return</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    }<br /></span></div><br /><br /><br /><br /><br />其他的一些考虑<br /><br /><strong>1.如何定义错误值？<br /></strong>简单的一点使用宏连续定义，like<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"><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> E_NO_FILE 1</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> E_BAD_FILE 2</span></div>复杂的一点就像COM，严格的定义每一位的意义<br />这种情况下你可以提供一个宏来创建错误代码,like<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"><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> MAKE_HRESULT(sev,fac,code)\</span><span style="COLOR: #000000"><br /><br />( (HRESULT) (((unsigned </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)(sev)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">31</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">|</span><span style="COLOR: #000000"> ((unsigned </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)(fac)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">16</span><span style="COLOR: #000000">) \<br /><br /></span><span style="COLOR: #000000">|</span><span style="COLOR: #000000"> ((unsigned </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)(code))) )<br /></span></div><br /><strong>2.可以提供一个宏或者函数来帮助判断是否成功<br /></strong>比如COM提供了FAILED宏来帮助你判断COM的返回值<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"><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> FAILED(Status)     ((HRESULT)(Status)&lt;0)</span><span style="COLOR: #000000"><br /></span></div><br /><strong>3.如果只有错误和成功两个返回值,考虑使用bool来返回<br /></strong>这个适用于C++,优点是意义很清晰.返回 true就是成功,false就是失败.<br /><br /><strong>4.要使用异常来表示错误的状态么?</strong><br />使用异常的好处就是返回值被省出来了，可以不返回或者返回其他信息，还有益于定义错误类型和简化程序流程。缺点就是C++对异常支持还不够好,没有finally,每一家编译器支持也不一样,实现可能大不同.<br /><img src ="http://www.cppblog.com/sandy/aggbug/6252.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-04-25 16:55 <a href="http://www.cppblog.com/sandy/archive/2006/04/25/6252.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Is it a vc6 bug?</title><link>http://www.cppblog.com/sandy/archive/2006/04/24/6195.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Mon, 24 Apr 2006 10:08:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/04/24/6195.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/6195.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/04/24/6195.html#Feedback</comments><slash:comments>21</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/6195.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/6195.html</trackback:ping><description><![CDATA[<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">
				<span style="COLOR: #000000">#include </span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">stdio.h</span>
				<span style="COLOR: #000000">&gt;</span>
				<span style="COLOR: #000000">
						<br />
						<br />
				</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> main()<br />{<br />    </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> a</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">10</span>
				<span style="COLOR: #000000">,b</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">20</span>
				<span style="COLOR: #000000">;<br /><br />    a</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">(a</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">b)</span>
				<span style="COLOR: #000000">-</span>
				<span style="COLOR: #000000">(b</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">a);<br /><br />    printf(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">a=%d,b=%d\n</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">,a,b);<br />    <br />    </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">;<br />}</span>
		</div>
		<br />很简单，交换a和b的值<br /><br />在debug模式下，输出<br />a=20,b=10<br /><br />在release模式下，输出<br />a=10,b=10<br /><br /><strong>I think there is a bug in release version.</strong><img src ="http://www.cppblog.com/sandy/aggbug/6195.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-04-24 18:08 <a href="http://www.cppblog.com/sandy/archive/2006/04/24/6195.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++代码静态分析工具-Prefast</title><link>http://www.cppblog.com/sandy/archive/2006/03/28/4689.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Tue, 28 Mar 2006 05:59:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/03/28/4689.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/4689.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/03/28/4689.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/4689.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/4689.html</trackback:ping><description><![CDATA[<p>
				<strong>1. 什么是Prefast</strong>
				<br />
				<br />Prefast是一种代码分析工具，它能够帮助你找到编译器不能找到的错误或者缺陷。Prefast首次被微软集成到Visual Studio 2005 Team Suite中去，使用起来非常方便。<br /><br /><strong>2.怎么使用Prefast</strong><br />在vs2005 Team Suite中，使用Prefast非常简单。修改你的工程属性，设置Enable Code Analysis For C/C++为Yes.<br /><br /><img height="414" alt="prefast1.jpg" src="http://www.cppblog.com/images/cppblog_com/sandy/prefast1.jpg" width="594" border="0" /><br /><br />效果:<br /><img height="624" alt="prefast2.jpg" src="http://www.cppblog.com/images/cppblog_com/sandy/prefast2.jpg" width="896" border="0" /><br /><br />注意到有可能错误的地方以浅灰色显示在编辑器中了。</p>
		<p>
				<strong>3.Prefast能帮你找到哪些错误<br /></strong>
				<br />1)没有初始化</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">
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">no initial</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> defect1()<br />{<br />        </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> a;<br />        </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> b;<br /><br />        b </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> a;<br />}</span>
		</div>
		<p>会报: d:\test\testcode\testcode.cpp(18) : warning C6001: Using uninitialized memory 'a': Lines: 15, 16, 18<br /><br />2)空指针取值<br /></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">
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">one path dereference NULL</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> defect4(</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> b, </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> c)<br />{<br />        </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">p </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> NULL;<br />        </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> a </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;<br /><br />        </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (b </span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">) {<br />                </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (c </span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">) {<br />                        p </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000">a;<br />                }<br />                </span>
				<span style="COLOR: #0000ff">else</span>
				<span style="COLOR: #000000"> {<br />                                                <br />                }<br />        }<br />        </span>
				<span style="COLOR: #0000ff">else</span>
				<span style="COLOR: #000000"> {<br />                </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (c </span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">) {<br /><br />                }<br />                </span>
				<span style="COLOR: #0000ff">else</span>
				<span style="COLOR: #000000"> {<br />                        p </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000">a;<br />                }<br />        }<br /><br />        </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">p;<br /><br />        </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000">;<br />}    <br /></span>
		</div>
		<p>会报:d:\test\testcode\testcode.cpp(65) : warning C6011: Dereferencing NULL pointer 'p': Lines: 45, 46, 48, 57, 65<br /><br />3)可能错误的运算符优先级<br /></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">
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> defect5()<br />{<br />        </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> a </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;<br />        </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> b </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;<br />        </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> c </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;<br /><br />        </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (a </span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000"> b </span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000"> c)<br />                </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000">;<br />}</span>
		</div>
		<p>会报: d:\test\testcode\testcode.cpp(76) : warning C6281: Incorrect order of operations: relational operators have higher precedence than bitwise operators<br /><br />4)可能的buffer overrun</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">
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> defect8()<br />{<br />        </span>
				<span style="COLOR: #0000ff">char</span>
				<span style="COLOR: #000000"> buf[</span>
				<span style="COLOR: #000000">100</span>
				<span style="COLOR: #000000">];<br />        </span>
				<span style="COLOR: #0000ff">char</span>
				<span style="COLOR: #000000"> buf2[</span>
				<span style="COLOR: #000000">200</span>
				<span style="COLOR: #000000">];<br />        </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> i </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">100</span>
				<span style="COLOR: #000000">;<br /><br />        sprintf(buf, </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">hello world %d</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">, i);<br />        strcpy(buf, buf2);<br />}</span>
		</div>
		<p>会报: d:\test\testcode\testcode.cpp(133) : warning C6202: Buffer overrun for 'buf', which is possibly stack allocated, in call to 'strcpy': length '200' exceeds buffer size '100'<br /><br />5)可能的无穷循环<br /><br /></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">
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">infinite loop</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> defect14()<br />{<br />        signed </span>
				<span style="COLOR: #0000ff">char</span>
				<span style="COLOR: #000000"> i;<br /><br />        </span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000"> (i </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">100</span>
				<span style="COLOR: #000000">; i </span>
				<span style="COLOR: #000000">&gt;=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">; i</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">) {<br />                ; <br />        }<br />}</span>
		</div>
		<p>会报: d:\test\testcode\testcode.cpp(198) : warning C6292: Ill-defined for-loop: counts up from maximum<br /><br />6)格式字符串错误<br /></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">
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">Format string mismatch</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> defect21()<br />{<br />        </span>
				<span style="COLOR: #0000ff">char</span>
				<span style="COLOR: #000000"> buff[</span>
				<span style="COLOR: #000000">5</span>
				<span style="COLOR: #000000">];<br />        sprintf(buff, </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">%s %s</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">a</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br />}</span>
		</div>
		<p>会报: d:\test\testcode\testcode.cpp(277) : warning C6063: Missing string argument to 'sprintf' that corresponds to conversion specifier '2'<br /><br />7)安全问题<br /></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">
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> defect27()<br />{<br />        CreateProcess(NULL,<br />               </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">c:\\program files\\Project.exe arg1</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">correct "\"c:\\program files\\Project.exe\" arg1",</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #000000">               NULL,<br />               NULL,<br />               </span>
				<span style="COLOR: #0000ff">false</span>
				<span style="COLOR: #000000">,<br />               </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">,<br />               NULL,<br />               NULL,<br />               NULL,<br />               NULL);               <br />}</span>
		</div>
		<p>会报: d:\test\testcode\testcode.cpp(327) : warning C6277: NULL application name with an unquoted path in call to 'CreateProcessA': results in a security vulnerability if the path contains spaces<br /><br />8)=和==误用<br /></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">
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> defect32()<br />{<br />        </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> a </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;<br /><br />        </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (a </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000">)<br />                </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000">;<br />}</span>
		</div>
		<p>会报: d:\test\testcode\testcode.cpp(405) : warning C6282: Incorrect operator: assignment of constant in Boolean context. Consider using '==' instead<br /><br />9)逻辑运算问题<br /></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">
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">always false</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> defect45()<br />{<br />        </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> x;<br /><br />        </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">&amp;&amp;</span>
				<span style="COLOR: #000000"> x</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">) {<br />                ;<br />        }<br />}</span>
		</div>
		<p>会报: d:\test\testcode\testcode.cpp(564) : warning C6237: (&lt;zero&gt; &amp;&amp; &lt;expression&gt;) is always zero. &lt;expression&gt; is never evaluated and might have side effects<br /><br />10)其他<br /><br /><br /><br /><br /><br /></p><img src ="http://www.cppblog.com/sandy/aggbug/4689.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-03-28 13:59 <a href="http://www.cppblog.com/sandy/archive/2006/03/28/4689.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++跨平台开发方法/工具</title><link>http://www.cppblog.com/sandy/archive/2006/03/24/4544.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Fri, 24 Mar 2006 08:45:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/03/24/4544.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/4544.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/03/24/4544.html#Feedback</comments><slash:comments>26</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/4544.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/4544.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 主要阐述linux/windows跨平台开发<br>&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2006/03/24/4544.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/4544.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-03-24 16:45 <a href="http://www.cppblog.com/sandy/archive/2006/03/24/4544.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>再谈从vc6迁移到vs2005</title><link>http://www.cppblog.com/sandy/archive/2006/03/13/4081.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Mon, 13 Mar 2006 03:03:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/03/13/4081.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/4081.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/03/13/4081.html#Feedback</comments><slash:comments>12</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/4081.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/4081.html</trackback:ping><description><![CDATA[<H1>&nbsp;</H1>
<P>&nbsp;&nbsp;&nbsp;作为C++编译器，从vc6到vc8最大的调整就是对C++标准的支持更好了。<BR>&nbsp;&nbsp;&nbsp;我发现的几点不同。<BR><BR>a. For 循环的声明<BR><BR><STRONG><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Vc6: for(int i&lt;0;i&lt;100;++i){}; j = i;<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>(ok)</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Vc8: <FONT color=#ff0000>for(int i&lt;0;i&lt;100;++i){}; j = i;<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>(illegal)</FONT></FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT size=2><FONT face="Courier New"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>int i; for(i&lt;0;i&lt;100;++i){}; j = i;<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>(ok)</FONT></FONT></STRONG></P>
<P>Vc8中的for循环中变量的有效期仅仅在for 循环的开始与结束期间有效。<BR><BR>b.string实现<BR>&nbsp;&nbsp;&nbsp;</P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Vc6: string s; char *p = s.begin(); (ok)</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Vc8: <FONT color=#ff0000>string s; char *p = s.begin(); (illegal)</FONT></FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT size=2><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>string s; char *p = const_cast&lt;char *&gt;(s.c_str()); (ok)</FONT></FONT></STRONG></P>
<P>在vc6中,string::iterator被定义为char *,但是vc8中不是<BR><BR>c.更严格的typename声明的需要<BR></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Vc6: </FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>template&lt;class T&gt;</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>class Test</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>{</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>public:</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt; TEXT-INDENT: 36pt"><STRONG><FONT face="Courier New" size=2>typedef map&lt;T,T&gt; mymap;</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT size=2><FONT face="Courier New"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>mymap::iterator mymap_iter;</FONT></FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>}; (ok)</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Vc8: </FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" color=#ff0000 size=2>template&lt;class T&gt;</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" color=#ff0000 size=2>class Test</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" color=#ff0000 size=2>{</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" color=#ff0000 size=2>public:</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT size=2><FONT face="Courier New"><FONT color=#ff0000><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>typedef map&lt;T,T&gt; mymap;</FONT></FONT></FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT size=2><FONT face="Courier New"><FONT color=#ff0000><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>mymap::iterator mymap_iter;</FONT></FONT></FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" color=#ff0000 size=2>};<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>(illegal)</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>typename mymap::iterator mymap_iter;(ok)</FONT></STRONG></P>
<P>vc8更加严格的要求程序员在类型前面加上typename以避免歧义<BR><BR>d.不允许默认的int类型<BR></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Vc6: fun() { return 0;} (ok)</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Vc8: <FONT color=#ff0000>fun() { return 0;} (illegal)</FONT></FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt; TEXT-INDENT: 30pt"><STRONG><FONT face="Courier New" size=2>int fun() { return 0;} (ok)</FONT></STRONG></P>
<P>vc8不支持默认返回int类型<BR><BR>e.typedef必须是public才能被外界访问到<BR><BR></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Vc6: </FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Class Test</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>{</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT size=2><FONT face="Courier New"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>typedef int iterator;</FONT></FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>}; </FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Test::iterator i; (ok)</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Vc8:</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" color=#ff0000 size=2>Class Test</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" color=#ff0000 size=2>{</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT size=2><FONT face="Courier New"><FONT color=#ff0000><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>typedef int iterator;</FONT></FONT></FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" color=#ff0000 size=2>}; </FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" color=#ff0000 size=2>Test::iterator i; (illegal)</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT size=2><FONT face="Courier New">&nbsp;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT></FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Class Test</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>{</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>public:</FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT size=2><FONT face="Courier New"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>typedef int iterator;</FONT></FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>}; </FONT></STRONG></P>
<P class=code style="MARGIN: 0cm 0cm auto 36pt"><STRONG><FONT face="Courier New" size=2>Test::iterator i; (ok)</FONT></STRONG></P>
<P>附录：一些资源（From msdn)<BR><BR><FONT face=宋体>Overviews:</FONT></P>
<UL type=disc>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l1 level1 lfo1; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/bw65k95a(en-us,vs.80).aspx">What's new in 8.0</A> </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l1 level1 lfo1; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/7223w6b0(en-us,vs.80).aspx">What's new in 7.1</A> </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l1 level1 lfo1; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/2byy0fh6(en-us,vs.80).aspx">What's new in 7.0</A></LI></UL>
<P><FONT face=宋体>Moving from 6.0 to 7.1:</FONT></P>
<UL type=disc>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l3 level1 lfo2; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/w1sc4t4k(en-us,vs.80).aspx">ATL/MFC Breaking changes (6.0 to 7.0)</A>&nbsp;(also seen <A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/vclrfatlcompatibilityissues.asp">here</A>&nbsp;but the list isn't as long) </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l3 level1 lfo2; tab-stops: list 36.0pt"><A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/vclrfatlcompatibilityissues.asp">ATL/MFC Breaking changes (7.0 to 7.1)</A> </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l3 level1 lfo2; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/zzz7ct0s(en-us,vs.80).aspx">Standard <a title="C++博客" href="http://www.cppblog.com/sandy" >c++</a> Library changes</A>&nbsp;and <A href="http://msdn2.microsoft.com/library/8h8eh904(en-us,vs.80).aspx">issues specific to upgrading</A> </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l3 level1 lfo2; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/ms177251(en-us,vs.80).aspx">Compiler breaking changes</A> </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l3 level1 lfo2; tab-stops: list 36.0pt">In-depth info on <A href="http://msdn2.microsoft.com/library/txbse2cf(en-us,vs.80).aspx">increased standards compliance</A> </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l3 level1 lfo2; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/kyb7zzw4(en-us,vs.80).aspx">Project upgrading</A></LI></UL>
<P><FONT face=宋体>Moving from 7.1 to 8.0:</FONT></P>
<UL type=disc>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l2 level1 lfo3; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/y8bt6w34(en-us,vs.80).aspx">Libraries breaking changes</A> </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l2 level1 lfo3; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/k3aehyyf(en-us,vs.80).aspx">ATL obsolete topics and deprecated functions with replacements</A> </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l2 level1 lfo3; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/96kas566(en-us,vs.80).aspx">MFC obsolete topics</A> </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l2 level1 lfo3; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/ms177253(en-us,vs.80).aspx">Compiler breaking changes</A> </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l2 level1 lfo3; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/z8dh4h17(en-us,vs.80).aspx">Deprecated compiler options</A> </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l2 level1 lfo3; tab-stops: list 36.0pt">Some removed linker options (mentioned <A href="http://msdn2.microsoft.com/library/hx072wbd(en-us,vs.80).aspx">here</A> and <A href="http://msdn2.microsoft.com/library/cx002e0b(en-us,vs.80).aspx">here</A>) </LI>
<LI class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l2 level1 lfo3; tab-stops: list 36.0pt"><A href="http://msdn2.microsoft.com/library/e54ke6de(en-us,vs.80).aspx">Preprocessor changes</A></LI></UL><img src ="http://www.cppblog.com/sandy/aggbug/4081.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-03-13 11:03 <a href="http://www.cppblog.com/sandy/archive/2006/03/13/4081.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java中最值得C++借鉴的特性s</title><link>http://www.cppblog.com/sandy/archive/2006/01/25/3018.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 25 Jan 2006 03:40:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/01/25/3018.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/3018.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/01/25/3018.html#Feedback</comments><slash:comments>16</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/3018.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/3018.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 讨论Java和C++&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2006/01/25/3018.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/3018.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-01-25 11:40 <a href="http://www.cppblog.com/sandy/archive/2006/01/25/3018.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>从全局变量到IOC模式</title><link>http://www.cppblog.com/sandy/archive/2006/01/20/2941.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Fri, 20 Jan 2006 09:23:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/01/20/2941.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/2941.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/01/20/2941.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/2941.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/2941.html</trackback:ping><description><![CDATA[很早以前，在我初学c语言的时候，我的第一个象样的程序是一个五子棋程序，使用了TC2.0的图形库，纯面向过程的设计，由上到下的设计，而且只有一个c文件就搞定了，大概几百行，可惜代码已经失传，非常可惜。<BR><BR>为什么要全局变量?<BR>List 1<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"><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;s1,s2,s3;<BR>fun1(s1);<BR>fun2(s1,s2);<BR>fun3(s1,s2,s3);<BR></SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>}<BR></SPAN></DIV>上面的s1,s2,s3如果改成全局变量，则变为<BR>List 2<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"><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;s1,s2,s3;<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;fun1();<BR>&nbsp;&nbsp;&nbsp;&nbsp;fun2();<BR>&nbsp;&nbsp;&nbsp;&nbsp;fun3();<BR>}</SPAN></DIV>似乎简洁了一些，而且没有了传递参数的开销。但是缺点也是很明显的，带了三个函数之间的耦合。<BR><BR>既然我们认识到全局变量的问题，怎么改进呢？<BR>代码1中由于有三个变量，如果有更多的，就更麻烦，我们可以这样改进<BR>List 3<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"><SPAN style="COLOR: #000000">typedef&nbsp;</SPAN><SPAN style="COLOR: #0000ff">struct</SPAN><SPAN style="COLOR: #000000">&nbsp;statusTag<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;s1,s2,s3;<BR>}Status;<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;Status&nbsp;s;<BR>&nbsp;&nbsp;&nbsp;&nbsp;fun1(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">s);<BR>&nbsp;&nbsp;&nbsp;&nbsp;fun2(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">s);<BR>&nbsp;&nbsp;&nbsp;&nbsp;fun3(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">s);<BR>&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></DIV>这种技巧你可以在lua中看到，看lua的使用代码<BR>List4<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"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">lua.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">lauxlib.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">lualib.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;argc,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">argv[])<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;lua_State&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">L&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;lua_open();<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</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">buf&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">var&nbsp;=&nbsp;100</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;var&nbsp;;<BR>&nbsp;&nbsp;&nbsp;&nbsp;luaopen_base(L);<BR>&nbsp;&nbsp;&nbsp;&nbsp;luaopen_io(L);<BR>&nbsp;&nbsp;&nbsp;&nbsp;lua_dostring(L,&nbsp;buf);<BR>&nbsp;&nbsp;&nbsp;&nbsp;lua_getglobal(L,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">var</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;lua_tonumber(L,&nbsp;</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;lua_close(L);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&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>}&nbsp;</SPAN></DIV>请注意到这里的lua_open方法，这其实是一种创建自己的工厂方法。不使用全局变量的好处就是，我们保留了可以创建多个对象的自由。<BR><BR>时代在发展，进入C++时代，但是全局变量仍然有人在用，存在就是合理的。GOF提出一种设计模式叫Singleton的模式，其核心思想就是不让全局变量漂浮在空中，把它放入class中，成为衣冠楚楚的C++公民。著名的Meyer Singleton像这样<BR>List 5<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"><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Status<BR>{<BR></SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;Status(){};<BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;Status</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;getInstance()<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;Status&nbsp;s;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;s;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>};<BR><BR></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;User<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;fun()<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">s&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;Status::Instance();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">.&nbsp;.&nbsp;.use&nbsp;s</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<BR>};</SPAN></DIV><BR>一切似乎很完美，使用private来防止client 创建它，保证了对象的唯一性(注意:Meyer singleton并不具有多线程安全，可能导致多次初始化对象)<BR><BR>但是随着 针对接口编程和单元测试越来越流行，singleton带来的对单元测试不友好的特点日益体现，全局变量不能很好的被mock，所以难于测试。<BR><BR>这时候所谓的IOC思想(<FONT size=2>Inversion of Control，即反转模式)</FONT>出来了，简单的来说，就是通过构造函数或者set方法实现注入<BR><BR>List6 - 构造函数注入<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"><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Status{};<BR><BR></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;User<BR>{<BR>&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User(Status&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">s):m_ps(s){};<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;fun()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">s&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;m_ps;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">m_ps;<BR>}</SPAN></DIV><BR>List7 - Set 注入<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"><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Status{};<BR><BR></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;User<BR>{<BR>&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User(){}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;setStaus(Status&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">s)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_ps&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;s;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;fun()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">s&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;m_ps;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">m_ps;<BR>}</SPAN></DIV><BR>使用IOC的好处是带来了更强大的灵活性，但是带来的问题就是调用者麻烦了(天下没有免费的午餐阿）<BR><BR>List8 <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"><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR>&nbsp;&nbsp;&nbsp;Status&nbsp;s;<BR>&nbsp;&nbsp;&nbsp;User&nbsp;u;<BR>&nbsp;&nbsp;&nbsp;u.setStatus(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">s);<BR>&nbsp;&nbsp;&nbsp;u.fun();<BR>&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></DIV><BR>好像一切又返朴归真，似乎并没有带来什么简单。有的时候简单和灵活性就是死对头。<BR><BR>为了简化用户进行手工注入，IOC容器出现，在Java世界里面，最著名的莫过于Spring了.IOC容器就像一个巨大的创建工厂，她可以使用xml来配置这些，这真的是一场革命。<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"><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">beans</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">bean&nbsp;id</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">status</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Status</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">bean</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">bean&nbsp;id</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">user</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">User</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">property&nbsp;name</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">status</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&gt;&lt;</SPAN><SPAN style="COLOR: #0000ff">ref</SPAN><SPAN style="COLOR: #000000">&nbsp;bean</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">status</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">/&gt;&lt;/</SPAN><SPAN style="COLOR: #000000">property</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">bean</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">beans</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN></DIV>Spring就是这样把注入的工作移到配置文件中去，提供了强大的灵活性和可配置性<BR><BR><BR>但是由于c/c++ 不具备的java那么多运行期的类型识别和反射的功能，所以我目前还没有发现有在C++中使用的IOC容器，如果你知道，请告诉我<BR><BR>那么如果是C++怎么来使注入变得简单一点呢，可以使用工厂方法了<BR><BR>List9<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"><SPAN style="COLOR: #000000">User&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;createUser(Status&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">s,Status2&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">s2)<BR>{<BR>&nbsp;&nbsp;&nbsp;User&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">user&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;User();<BR>&nbsp;&nbsp;&nbsp;user</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">setStatus(s);<BR>&nbsp;&nbsp;&nbsp;user</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">setStatus2(s2);<BR>&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;user;<BR>}<BR></SPAN></DIV><BR><BR>总结:<BR>其实软件的设计根本就没有所谓的黄金法则，没有免费的午餐，你在获得更强大的灵活性，往往都得到复杂性的附加效果。如果你就是写一个自己玩的小游戏，ok,你就是用全局变量。如果你要设计庞大的Office,那你就没有办法把代码写的很简单，因为你要考虑的东西多了，可维护性，可测试性。<img src ="http://www.cppblog.com/sandy/aggbug/2941.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-01-20 17:23 <a href="http://www.cppblog.com/sandy/archive/2006/01/20/2941.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++编译期Constraints设计</title><link>http://www.cppblog.com/sandy/archive/2006/01/17/2830.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Tue, 17 Jan 2006 06:12:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/01/17/2830.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/2830.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/01/17/2830.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/2830.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/2830.html</trackback:ping><description><![CDATA[所谓的compile-time Constraints就是在编译期使用C++的语法规则对设计进行约束检查，尽量在编译期把程序的问题暴露出来，节省成本。<BR><BR>常用的规则:<BR>1.<STRONG>大小为0的数组在编译期会报错<BR></STRONG>
<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"><SPAN style="COLOR: #000000">template</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">typename&nbsp;T1,typename&nbsp;T2</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;check_size()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;dmp[</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(T1)</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(T2)];<BR>}</SPAN></DIV><BR>比如 <BR>check_size&lt;int,long&gt;();//ok<BR>check_size&lt;int,char&gt;();//compile error:cannot allocate an array of constant size 0<BR><BR><STRONG>2.不同类型的指针需要强制转换</STRONG><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"><SPAN style="COLOR: #000000">template</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">typename&nbsp;T1,typename&nbsp;T2</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;is_derived(T1&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pd =0 ,T2&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pb=0)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;pb&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;pd;<BR>}</SPAN></DIV>比如:<BR>is_derived&lt;int,char&gt;()&nbsp; //compile failed<BR><BR><STRONG>3.union的成员不能是非POD</STRONG><BR>union Test<BR>{<BR>&nbsp;&nbsp;&nbsp; std::string s; //illegal<BR>};<BR>这一点可以用来测试一个类型是否是POD<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"><SPAN style="COLOR: #000000">template</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">typename&nbsp;T</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;check_pod()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;union&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;data;<BR>&nbsp;&nbsp;&nbsp;&nbsp;};<BR>};</SPAN></DIV>比如:<BR>check_pod&lt;int&gt;();//ok<BR>check_pod&lt;std::string&gt;();//failed<BR><BR><BR>常用的技巧:<BR><BR><STRONG>1.利用模板特化</STRONG><BR>Loki里面的例子<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"><SPAN style="COLOR: #0000ff">namespace</SPAN><SPAN style="COLOR: #000000">&nbsp;Loki<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;template</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">struct</SPAN><SPAN style="COLOR: #000000">&nbsp;CompileTimeError;<BR>&nbsp;&nbsp;&nbsp;&nbsp;template</SPAN><SPAN style="COLOR: #000000">&lt;&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">struct</SPAN><SPAN style="COLOR: #000000">&nbsp;CompileTimeError</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;{};<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">#define</SPAN><SPAN style="COLOR: #000000">&nbsp;LOKI_STATIC_CHECK(expr,&nbsp;msg)&nbsp;\</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Loki::CompileTimeError</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">((expr)&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;ERROR_##msg;&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">)ERROR_##msg;&nbsp;}&nbsp;</SPAN></DIV>利用特化来定制错误信息<BR><BR>2.<STRONG>利用空函数来实现0开销</STRONG><BR>编译器可能将空函数优化掉<BR><BR>3.<STRONG>利用enum无开销的取得编译期的所得到的整数值<BR></STRONG>Loki里面的例子:<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"><SPAN style="COLOR: #000000">template&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;TList</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">struct</SPAN><SPAN style="COLOR: #000000">&nbsp;Length;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">struct</SPAN><SPAN style="COLOR: #000000">&nbsp;Length</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">NullType</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">enum</SPAN><SPAN style="COLOR: #000000">&nbsp;{&nbsp;value&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">&nbsp;};<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;T,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;U</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">struct</SPAN><SPAN style="COLOR: #000000">&nbsp;Length</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;Typelist</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">T,&nbsp;U</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">enum</SPAN><SPAN style="COLOR: #000000">&nbsp;{&nbsp;value&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;Length</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">U</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">::value&nbsp;};<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<BR></SPAN></DIV><img src ="http://www.cppblog.com/sandy/aggbug/2830.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-01-17 14:12 <a href="http://www.cppblog.com/sandy/archive/2006/01/17/2830.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>什么才是多线程安全的</title><link>http://www.cppblog.com/sandy/archive/2006/01/12/2690.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Thu, 12 Jan 2006 09:45:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/01/12/2690.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/2690.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/01/12/2690.html#Feedback</comments><slash:comments>19</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/2690.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/2690.html</trackback:ping><description><![CDATA[对于多线程编程，很多人概念不清，写代码的时候要么是处处加锁，影响性能不说，还容易莫名其妙的死锁，还有人对多线程敬而远之。<BR><BR>所以学习多线程编程最重要的不是学习API,而是理解什么才是多线程安全的代码<BR><BR>从例子说起<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"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">windows.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">process.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><BR></SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">&nbsp;global1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #0000ff">volatile</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">&nbsp;global2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR><BR></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;MyClass<BR>{<BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;MyClass()&nbsp;:&nbsp;m(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">m;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;fun(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;v)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;m</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">v;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-----------9</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;v)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;v;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------------10</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;m;<BR>};<BR><BR>MyClass&nbsp;global_object;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------------8</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR>unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;__stdcall&nbsp;thread_fun1(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">param)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;static2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;MyClass&nbsp;static_object;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">--------6</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;local1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">local1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------1</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">static2;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------2</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">global1;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------3</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">global2;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------4</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;InterlockedIncrement(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">global1);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">--------5</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;local1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;global_object.fun(local1);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">----------7</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;global_object.</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">(local1);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">---------------11</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR>&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>}<BR><BR><BR>unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;__stdcall&nbsp;thread_fun2(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">param)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">global1;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------3</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">global2;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-------4</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;InterlockedIncrement(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">global1);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">--------5</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;global_object.</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">-----------11</SPAN><SPAN style="COLOR: #008000"><BR></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>}<BR><BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;HANDLE&nbsp;thread1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(HANDLE)_beginthreadex(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">thread_fun1,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">thread&nbsp;1</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;HANDLE&nbsp;thread2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(HANDLE)_beginthreadex(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">thread_fun1,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">thread&nbsp;2</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;HANDLE&nbsp;thread3&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(HANDLE)_beginthreadex(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">thread_fun2,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">thread&nbsp;3</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;WaitForSingleObject(thread1,INFINITE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;WaitForSingleObject(thread2,INFINITE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;WaitForSingleObject(thread3,INFINITE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&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>}<BR><BR></SPAN></DIV><BR><BR><STRONG>1.局部变量局部使用是安全的</STRONG><BR>为什么?因为每个thread 都有自己的运行堆栈，而局部变量是生存在堆栈中,大家不干扰。<BR>所以代码1<BR>int local1;<BR>++local1;<BR>是安全的<BR><BR><STRONG>2.全局原生变量多线程读写是不安全的<BR></STRONG>全局变量是在堆(heap)中<BR>long global1 = 0;<BR>++global2;<BR>++这个操作其实分为两部，一个是读，另外一个是写<BR>&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ecx,global<BR>&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ecx,1<BR>&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; global,ecx<BR>所以代码3处是不安全的<BR><BR><STRONG>3.函数静态变量多线程读写也是不安全的<BR></STRONG>道理同2<BR>所以代码2处也是不安全的<BR><BR><STRONG>4.volatile能保证全局整形变量是多线程安全的么</STRONG><BR>不能。<BR>volatile仅仅是告诫compiler不要对这个变量作优化，每次都要从memory取数值，而不是从register<BR>所以代码4也不是安全<BR><BR><STRONG>5.InterlockedIncrement保证整型变量自增的原子性</STRONG><BR>所以代码5是安全的<BR><BR><STRONG>6.function static object的初始化是多线程安全的么</STRONG><BR>不是。<BR>著名的Meyer Singleton其实不是线程安全的<BR>Object &amp; getInstance()<BR>{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; static Object o;<BR>&nbsp;&nbsp;&nbsp;&nbsp; return o;<BR>}<BR>可能会造成多次初始化对象<BR>所以代码6处是不安全的<BR><BR><STRONG>7.在32机器上，4字节整形一次assign是原子的</STRONG><BR>比如<BR>i =10; //thread1<BR>i=4; //thread2<BR>不会导致i的值处于未知状态,要么是10要么是4<BR><BR>其它的大家自己去体会。<BR><BR>写好多线程安全的法宝就是封装，使数据有保护的被访问到<BR>安全性：<BR>局部变量&gt;成员变量&gt;全局变量<img src ="http://www.cppblog.com/sandy/aggbug/2690.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-01-12 17:45 <a href="http://www.cppblog.com/sandy/archive/2006/01/12/2690.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>诡异的C</title><link>http://www.cppblog.com/sandy/archive/2006/01/09/2504.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Mon, 09 Jan 2006 06:04:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/01/09/2504.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/2504.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/01/09/2504.html#Feedback</comments><slash:comments>12</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/2504.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/2504.html</trackback:ping><description><![CDATA[<P><STRONG>1.无穷loop</STRONG><BR></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"><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i,a[</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">];<BR></SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(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">10</SPAN><SPAN style="COLOR: #000000">;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">i)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">&nbsp;;<BR>}<BR></SPAN></DIV>
<P>why?<BR>函数运行时堆栈：<BR>a[0] a[1] ....a[9] i<BR>a[10] = 0;改变i的值为0,造成无穷循环</P>
<P><STRONG>2.八进制的误用</STRONG></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"><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i[]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">012</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #000000">024</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #000000">125</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};</SPAN></DIV>
<P><BR>为了对齐可能造成的错误，变成了八进制</P>
<P><STRONG>3.如何调用地址为0的过程？<BR></STRONG>(*(void(*)())0)();<BR>ps:在大多数现代操作系统，进程引用的地址为虚拟地址，所以这个调用可能合法</P>
<P><BR><STRONG>4.运算符的优先级</STRONG><BR></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"><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">1</SPAN><SPAN style="COLOR: #000000">;<BR>i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;i</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN></DIV>
<P>这时i的值是多少？<BR>不是18，是64，因为 i=i&lt;&lt;4+2; &lt;==&gt; i=i&lt;&lt;(4+2);</P>
<P><STRONG>5.悬挂的if</STRONG><BR></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"><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(x</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">1</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(y</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;error();&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">2</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">{&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">3</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">x</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">y;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">z);<BR>}</SPAN></DIV>
<P>C中的else总是和最近的if匹配，所以3和2匹配，而不是和1匹配。等价于<BR></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"><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(x</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">1</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(y</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;error();&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">2</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">{&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">3</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">x</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">y;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">z);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}<BR></SPAN></DIV>
<P><BR>解决方法：总是使用{}和if配对</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P><img src ="http://www.cppblog.com/sandy/aggbug/2504.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-01-09 14:04 <a href="http://www.cppblog.com/sandy/archive/2006/01/09/2504.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++性能优化实践2---选择合适的容器</title><link>http://www.cppblog.com/sandy/archive/2006/01/05/2448.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Thu, 05 Jan 2006 05:07:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/01/05/2448.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/2448.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/01/05/2448.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/2448.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/2448.html</trackback:ping><description><![CDATA[有很多性能问题，在系统使用的初期，不大能看出来，因为使用量的很小。随着系统的不断深入使用，性能问题才出现，尤其是那种24*7都要不停运行的程序。<BR><BR>下面的一个例子，是经常在项目中都会用到的.ini配置文件生成和解析的过程<BR>比如<BR><BR>[section1]<BR>&nbsp;&nbsp;&nbsp;key1 = value1&nbsp;;here is comment<BR>&nbsp;&nbsp; key2 = value2<BR>[section2]<BR>&nbsp;&nbsp;&nbsp;key3 = value3<BR>&nbsp;&nbsp; key4 = value4<BR><BR>当然WinAPI也提供了<STRONG>WritePrivateProfileString</STRONG>,<STRONG>GetPrivateProfileString</STRONG>两个API来读写这种文件格式，但是<BR>每次使用都会打开文件来读写，性能非常低，只适用于小规模的数据读写。<BR><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"><SPAN style="COLOR: #0000ff">#define</SPAN><SPAN style="COLOR: #000000">&nbsp;_TD_INI_H_</SPAN><SPAN style="COLOR: #000000"><BR><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">list</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">fstream</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></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: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;KEV_VALUE<BR>{<BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;KEV_VALUE(){m_bIsGarbage</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">FALSE;m_bSingleLine</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">FALSE;};<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">virtual</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN style="COLOR: #000000">KEV_VALUE(){};<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;m_key;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;m_value;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;m_bIsGarbage;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;m_bSingleLine;<BR>};<BR>typedef&nbsp;list</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">KEV_VALUE</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;LIST_KEY_VELUE;<BR><BR></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;SECTION<BR>{<BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SECTION(){};<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">virtual</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN style="COLOR: #000000">SECTION(){};<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;m_section;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LIST_KEY_VELUE&nbsp;m_listKeyValue;<BR>};<BR>typedef&nbsp;list</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">SECTION</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;LIST_SECTION;<BR><BR></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;CTDIni&nbsp;&nbsp;<BR>{<BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CTDIni(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">virtual</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN style="COLOR: #000000">CTDIni(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;UpdateData(&nbsp;BOOL&nbsp;bSave</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;SetFileName(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrFileName&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;GetLastSectionName(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;str&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;AddSection(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;DeleteSection(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;ReadSection(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpszSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;str&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;SetKeyValue(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKey,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrValue&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;SetKeyValue(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKey,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;INT32&nbsp;nValue&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;GetKeyValue(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKey,&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrValue&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;DeleteKeyValue(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKey&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;ChangeKeyName(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKeyOld,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKeyNew&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;ChangeSectionName(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSectionOld,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSectionNew&nbsp;);<BR></SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LIST_SECTION&nbsp;m_sections;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;m_strFileName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;AddGarbage(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrGarbage,&nbsp;BOOL&nbsp;bSingleLine</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">FALSE&nbsp;);<BR>};<BR><BR></SPAN><SPAN style="COLOR: #0000ff">#endif</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;!defined(_TD_INI_H_)</SPAN></DIV><BR>这个是解析类的声明，重要的是它的数据结构，它采用了两级链表的结构，第一级是所有section的list,第二级是section下面的key-value的list.<BR>下面是其中的实现代码片断<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"><SPAN style="COLOR: #000000">BOOL&nbsp;CTDIni::SetKeyValue(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKey,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrValue&nbsp;)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LIST_SECTION::iterator&nbsp;i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;m_sections.begin();&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;m_sections.end();&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">&nbsp;)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(&nbsp;(</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">i).m_section&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;lpstrSection&nbsp;)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<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;LIST_KEY_VELUE::iterator&nbsp;j;<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;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(&nbsp;j&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">i).m_listKeyValue.begin();&nbsp;j&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">i).m_listKeyValue.end();&nbsp;j</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">&nbsp;)<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;{<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;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(&nbsp;(</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">j).m_key&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;lpstrKey&nbsp;)<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;{<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;(</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">j).m_value&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;lpstrValue;<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;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;TRUE;<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;}<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;}<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;KEV_VALUE&nbsp;tmp;<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;tmp.m_key&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;lpstrKey;<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;tmp.m_value&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;lpstrValue;<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;(</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">i).m_listKeyValue.push_back(&nbsp;tmp&nbsp;);<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;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;TRUE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;FALSE;<BR>}</SPAN></DIV><BR>上面的一个方法是添加一个值到配置中去，它的算法是首先查找它所在的section,然后查找所在的key,<BR>最后决定是insert还是update.<BR><BR>这样性能问题就来了，当数据不断增加，SetKeyValue所需要的时间呈N*N的方式增长。很可怕。CPU的占用率也会跑到很高。<BR><BR>最后，我们不得不进行优化，因为在我们的项目中，不存在相同的section，也没有相同的key,所以我们使用map，使得查找时间变成常数级别。(即使有相同的key&amp;section,也可以使用multimap)<BR><BR>优化后的数据结构是这样的<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"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">stdio.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">list</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">map</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">fstream</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><BR></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><BR></SPAN><SPAN style="COLOR: #0000ff">struct</SPAN><SPAN style="COLOR: #000000">&nbsp;VELUE<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;m_value;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;m_bIsGarbage;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;m_bSingleLine;<BR>};<BR><BR>typedef&nbsp;std::map</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">std::</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">,VELUE</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;MAP_KEY_VELUE;<BR><BR>typedef&nbsp;std::map</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">std::</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">,MAP_KEY_VELUE</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;MAP_SECTION;<BR><BR></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;CTDIni&nbsp;&nbsp;<BR>{<BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CTDIni(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">virtual</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN style="COLOR: #000000">CTDIni(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;UpdateData(&nbsp;BOOL&nbsp;bSave</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;SetFileName(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrFileName&nbsp;);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;GetLastSectionName(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;str&nbsp;);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;AddSection(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;DeleteSection(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;ReadSection(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpszSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;str&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;IsExistSection(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;SetKeyValue(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKey,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrValue&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;SetKeyValue(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKey,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;INT32&nbsp;nValue&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;GetKeyValue(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKey,&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrValue&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;DeleteKeyValue(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKey&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;ChangeKeyName(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKeyOld,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKeyNew&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;ChangeSectionName(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSectionOld,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSectionNew&nbsp;);<BR></SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAP_SECTION&nbsp;m_sections;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;m_strFileName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;AddGarbage(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrGarbage,&nbsp;BOOL&nbsp;bSingleLine</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">FALSE&nbsp;);<BR>};</SPAN></DIV><BR><BR>SetKeyValue那个方法的实现是这样：<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"><SPAN style="COLOR: #000000">BOOL&nbsp;CTDIni::SetKeyValue(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrSection,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrKey,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lpstrValue&nbsp;)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAP_SECTION::iterator&nbsp;i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAP_KEY_VELUE::iterator&nbsp;j;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;m_sections.find(lpstrSection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;m_sections.end())<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;FALSE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;i</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">second.find(lpstrKey);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(&nbsp;j&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;i</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">second.end())&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">update</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">second.m_value&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;lpstrValue;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">insert</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VELUE&nbsp;tmp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp.m_value&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;lpstrValue;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp.m_bSingleLine&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp.m_bIsGarbage&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">second[lpstrKey]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;tmp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;TRUE;<BR>}</SPAN></DIV><BR>两者的性能差距有多大？超过你的想象<img src ="http://www.cppblog.com/sandy/aggbug/2448.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-01-05 13:07 <a href="http://www.cppblog.com/sandy/archive/2006/01/05/2448.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>利用boost::serialization来实现C++ RMI</title><link>http://www.cppblog.com/sandy/archive/2005/12/29/2236.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Thu, 29 Dec 2005 06:42:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2005/12/29/2236.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/2236.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2005/12/29/2236.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/2236.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/2236.html</trackback:ping><description><![CDATA[当我第一次使用boost::serlization的时候，就意识到它在RMI(Remote method Invoke)的意义，因为JAVA RMI就是建立在java serlization的基础之上。<BR><BR>先来个boost::serlization使用的例子<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"><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">boost</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">archive</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">text_oarchive.hpp</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">boost</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">archive</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">text_iarchive.hpp</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">boost</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">archive</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">xml_oarchive.hpp</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;test_serialization()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;boost::archive::text_oarchive&nbsp;to(cout&nbsp;,&nbsp;boost::archive::no_header);<BR>&nbsp;&nbsp;&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">10</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;s&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\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;to&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;to&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;s;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;ofstream&nbsp;f(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">test.xml</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;boost::archive::xml_oarchive&nbsp;xo(f);<BR>&nbsp;&nbsp;&nbsp;&nbsp;xo&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;BOOST_SERIALIZATION_NVP(i)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;BOOST_SERIALIZATION_NVP(s);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;boost::archive::text_iarchive&nbsp;ti(cin&nbsp;,&nbsp;boost::archive::no_header);<BR>&nbsp;&nbsp;&nbsp;&nbsp;ti&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;s;<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">i=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">s=</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;s&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;endl;<BR>}<BR></SPAN></DIV><BR>假如我们可以将object序列化以后通过socket传过去，就可以实现RMI了。<BR>当然这种方法只能是在C++程序之间传递。<BR><BR>今天在codeproject上面发现已经有人已经这样做了，原来早就有人跟我有一样的想法<BR>看看作者封装的结果吧<BR><BR>server端:<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"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">RCF</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">RCF.hpp</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;<BR><BR>RCF_BEGIN(I_Echo,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">I_Echo</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;RCF_METHOD_R1(std::</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">,&nbsp;echo,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;std::</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">);<BR>RCF_END(I_Echo);<BR><BR></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Echo<BR>{<BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;std::</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;echo(</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;std::</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">msg)&nbsp;{&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;msg;&nbsp;}<BR>};<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;port&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">50001</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;RCF::RcfServer&nbsp;server(port);<BR>&nbsp;&nbsp;server.bind</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">I_Echo,&nbsp;Echo</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">();<BR>&nbsp;&nbsp;server.start();<BR>&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></DIV><BR>client端：<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"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">RCF</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">RCF.hpp</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><BR>RCF_BEGIN(I_Echo,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">I_Echo</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;RCF_METHOD_R1(std::</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">,&nbsp;echo,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;std::</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">);<BR>RCF_END(I_Echo);<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR>&nbsp;&nbsp;std::cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;RcfClient</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">I_Echo</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">localhost</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;<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;</SPAN><SPAN style="COLOR: #000000">50001</SPAN><SPAN style="COLOR: #000000">).echo(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">my&nbsp;message</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&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></DIV><BR>比较简洁,利用了boost强大的function,thread,serlization<BR><BR><BR>RCF下载地址:&nbsp; <A href="http://www.codeproject.com/threads/RMI_For_Cpp/RCF.zip">http://www.codeproject.com/threads/RMI_For_Cpp/RCF.zip</A><BR><BR>介绍： <A href="http://www.codeproject.com/threads/RMI_For_Cpp.asp">http://www.codeproject.com/threads/RMI_For_Cpp.asp</A><BR><BR><BR><img src ="http://www.cppblog.com/sandy/aggbug/2236.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2005-12-29 14:42 <a href="http://www.cppblog.com/sandy/archive/2005/12/29/2236.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++性能优化实践1---设计数据结构</title><link>http://www.cppblog.com/sandy/archive/2005/12/22/1973.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Thu, 22 Dec 2005 12:42:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2005/12/22/1973.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/1973.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2005/12/22/1973.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/1973.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/1973.html</trackback:ping><description><![CDATA[<P>对于性能优化，相信喜欢C++的人都是比较重视效率。我准备写一个系列，主要是基于我的一些实践，至于理论上的东西，不是我的重点。我也讲不出来一堆道理:-)。我所说的例子，都将是我以前所遇到的一些案例，记录一下思路，拿出来跟大家share。<BR><BR><BR>在C++程序中，自从有了STL,很少有人去自己去设计数据结构，当然大部分情况STL的效率都是可以的。万事都有例外。<BR><BR>我接触到一个需求，是根据手机号码的号段表(位数不定，一般是七八位）来查出手机号码所在的地区。<BR>比如<BR>1315156&nbsp;&nbsp; 南京<BR>13812345 北京<BR>1366789&nbsp; 上海<BR>025&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 南京<BR><BR>一种可能的方案是设计多个hashmap<BR>map&lt;int,string&gt; seg8; map&lt;int,string&gt; seg7;&nbsp; map&lt;int,string&gt; seg3; ...<BR>每个map分别对应不同的位数。对于一个phone,分别在这些map中查找<BR>但是效率不行。<BR><BR>这里面有个关键，因为手机号码都是数字，我们可以设计出一棵树，每个节点都是0-9，他可以有10个节点，叶子节点我们就可以储存数据。迅速在大脑里想象一个这样的模型。<BR><BR>还是看代码比较明了。 <FONT color=#0000ff><STRONG>十叉树</STRONG></FONT><BR><BR></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"><SPAN style="COLOR: #0000ff">#if</SPAN><SPAN style="COLOR: #000000">&nbsp;!defined&nbsp;__TREE10__</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">#define</SPAN><SPAN style="COLOR: #000000">&nbsp;__TREE10__</SPAN><SPAN style="COLOR: #000000"><BR><BR>#pragma&nbsp;warning(disable:</SPAN><SPAN style="COLOR: #000000">4786</SPAN><SPAN style="COLOR: #000000">)<BR><BR></SPAN><SPAN style="COLOR: #0000ff">namespace</SPAN><SPAN style="COLOR: #000000">&nbsp;sandy<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">namespace</SPAN><SPAN style="COLOR: #000000">&nbsp;Private&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;VALUE</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">struct</SPAN><SPAN style="COLOR: #000000">&nbsp;__Node10&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">节点struct</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;__Node10</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">VALUE</SPAN><SPAN style="COLOR: #000000">&gt;*</SPAN><SPAN style="COLOR: #000000">&nbsp;POINTER;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUE&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;data;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">数据</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;POINTER&nbsp;ptr[</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">];&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">子节点</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__Node10():data(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(ptr,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(POINTER)</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;template</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">typename&nbsp;VALUE</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;CTree10<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp; Private::__Node10</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">VALUE</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;NODE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_lcount;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">插入的数据数量</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_lnodecount;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">节点数</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NODE</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;&nbsp;&nbsp;&nbsp;m_proot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">根结点指针</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CTree10():m_lcount(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">),m_lnodecount(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">),m_proot(CreateNode())&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">notice:Keep&nbsp;order&nbsp;with&nbsp;their&nbsp;declare</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN style="COLOR: #000000">CTree10()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DestroyAll();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;DestroyAll()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">short</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</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">10</SPAN><SPAN style="COLOR: #000000">;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">i)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(m_proot</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">ptr[i]</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Remove(m_proot</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">ptr[i]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_proot</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">ptr[i]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">bool</SPAN><SPAN style="COLOR: #000000">&nbsp;Insert(</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pKey,</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;VALUE&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">data)&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">插入节点</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert(pKey</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NODE&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pNode&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;m_proot;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NODE&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pChildNode&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;c&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(unsigned&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">strlen(pKey);</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">i)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;pKey[i];&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(c</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">||</SPAN><SPAN style="COLOR: #000000">&nbsp;c</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">9</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pChildNode&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;pNode</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">ptr[(c</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">)];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(pChildNode&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">not&nbsp;build</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pChildNode&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;pNode</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">ptr[(c</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">)]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;CreateNode();</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">create&nbsp;a&nbsp;new&nbsp;child</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pNode&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;pChildNode&nbsp;;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">change&nbsp;node&nbsp;to&nbsp;child&nbsp;node</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(pNode</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">data&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">empty&nbsp;</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pNode</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">data&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;VALUE(data);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">m_lcount;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">already&nbsp;inserted</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">bool</SPAN><SPAN style="COLOR: #000000">&nbsp;Lookup(</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pKey,VALUE&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">data,</SPAN><SPAN style="COLOR: #0000ff">bool</SPAN><SPAN style="COLOR: #000000">&nbsp;strick&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert(pKey</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NODE&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pNode&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;m_proot;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NODE&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pChildNode&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;c&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(unsigned&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">strlen(pKey);</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">i)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;pKey[i];&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(c</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">||</SPAN><SPAN style="COLOR: #000000">&nbsp;c</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">9</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pChildNode&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;pNode</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">ptr[(c</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">)];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(pChildNode</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pNode&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;pChildNode;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;can't&nbsp;find</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">strick)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<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;</SPAN><SPAN style="COLOR: #0000ff">break</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(pNode</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">data&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">already&nbsp;inserted</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">(pNode</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">data);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;no&nbsp;inserted</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NODE&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">CreateNode()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NODE&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pNewNode&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;NODE();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert(pNewNode</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">m_lnodecount;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;pNewNode;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;Remove(NODE&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pNode)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert(pNode</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">short</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">&nbsp;;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">&nbsp;;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">&nbsp;)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(&nbsp;pNode&nbsp;</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;ptr[&nbsp;i&nbsp;]&nbsp;)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Remove(&nbsp;pNode&nbsp;</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;ptr[&nbsp;i&nbsp;]&nbsp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(pNode</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">data</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;pNode</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">data;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">--</SPAN><SPAN style="COLOR: #000000">m_lcount;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">--</SPAN><SPAN style="COLOR: #000000">m_lnodecount;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;pNode;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;};<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">#endif</SPAN></DIV>
<P><BR>这个Tree10我在vc6下测试的结果：<BR>插入速度比std::map快3倍，查询速度则是std::map的10倍<BR><BR><BR>&nbsp;<BR></P><img src ="http://www.cppblog.com/sandy/aggbug/1973.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2005-12-22 20:42 <a href="http://www.cppblog.com/sandy/archive/2005/12/22/1973.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>vc6函数调用浅析</title><link>http://www.cppblog.com/sandy/archive/2005/12/14/1745.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 14 Dec 2005 08:53:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2005/12/14/1745.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/1745.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2005/12/14/1745.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/1745.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/1745.html</trackback:ping><description><![CDATA[今天研究了一下vc6函数调用，看看vc6调用函数时候都做了什么。有些意思。<BR><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"><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;fun(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;a,</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;b)<BR>{<BR>&nbsp;&nbsp;&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">3</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;a</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">b</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">i;<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;a&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,b</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;result&nbsp;;<BR>&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;fun(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;result;<BR>}</SPAN></DIV><BR>非常简单。反汇编后(Debug版)变成这样<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"><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;fun(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;a,</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;b)<BR></SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;{<BR></SPAN><SPAN style="COLOR: #000000">00401020</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ebp<BR></SPAN><SPAN style="COLOR: #000000">00401021</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ebp,esp<BR></SPAN><SPAN style="COLOR: #000000">00401023</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;sub&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esp,44h<BR></SPAN><SPAN style="COLOR: #000000">00401026</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ebx<BR></SPAN><SPAN style="COLOR: #000000">00401027</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esi<BR></SPAN><SPAN style="COLOR: #000000">00401028</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edi<BR></SPAN><SPAN style="COLOR: #000000">00401029</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;lea&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edi,[ebp</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">44h]<BR>0040102C&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ecx,11h<BR></SPAN><SPAN style="COLOR: #000000">00401031</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eax,0CCCCCCCCh<BR></SPAN><SPAN style="COLOR: #000000">00401036</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;rep&nbsp;stos&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[edi]<BR></SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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">3</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #000000">00401038</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[ebp</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">],</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;a</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">b</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">i;<BR>0040103F&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eax,dword&nbsp;ptr&nbsp;[ebp</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">8</SPAN><SPAN style="COLOR: #000000">]<BR></SPAN><SPAN style="COLOR: #000000">00401042</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eax,dword&nbsp;ptr&nbsp;[ebp</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">0Ch]<BR></SPAN><SPAN style="COLOR: #000000">00401045</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eax,dword&nbsp;ptr&nbsp;[ebp</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">]<BR></SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;}<BR>00401048&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; edi<BR>00401049&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; esi<BR>0040104A&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ebx<BR>0040104B&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; esp,ebp<BR>0040104D&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ebp<BR>0040104E&nbsp;&nbsp; ret<BR><BR></SPAN><SPAN style="COLOR: #000000">7</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR></SPAN><SPAN style="COLOR: #000000">8</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;{<BR></SPAN><SPAN style="COLOR: #000000">00401060</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ebp<BR></SPAN><SPAN style="COLOR: #000000">00401061</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ebp,esp<BR></SPAN><SPAN style="COLOR: #000000">00401063</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;sub&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esp,4Ch<BR></SPAN><SPAN style="COLOR: #000000">00401066</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ebx<BR></SPAN><SPAN style="COLOR: #000000">00401067</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esi<BR></SPAN><SPAN style="COLOR: #000000">00401068</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edi<BR></SPAN><SPAN style="COLOR: #000000">00401069</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;lea&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edi,[ebp</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">4Ch]<BR>0040106C&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ecx,13h<BR></SPAN><SPAN style="COLOR: #000000">00401071</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eax,0CCCCCCCCh<BR></SPAN><SPAN style="COLOR: #000000">00401076</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;rep&nbsp;stos&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[edi]<BR></SPAN><SPAN style="COLOR: #000000">9</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;a&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,b</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #000000">00401078</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[ebp</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">],</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000"><BR>0040107F&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[ebp</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">8</SPAN><SPAN style="COLOR: #000000">],</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;result&nbsp;;<BR></SPAN><SPAN style="COLOR: #000000">11</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;fun(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #000000">00401086</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #000000">00401088</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000"><BR>0040108A&nbsp;&nbsp;&nbsp;call&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ILT</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">(fun)&nbsp;(0040100a)<BR>0040108F&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esp,</SPAN><SPAN style="COLOR: #000000">8</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #000000">00401092</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[ebp</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">0Ch],eax<BR></SPAN><SPAN style="COLOR: #000000">12</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;result;<BR></SPAN><SPAN style="COLOR: #000000">00401095</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eax,dword&nbsp;ptr&nbsp;[ebp</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">0Ch]<BR></SPAN><SPAN style="COLOR: #000000">13</SPAN><SPAN style="COLOR: #000000">:&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #000000">00401098</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edi<BR></SPAN><SPAN style="COLOR: #000000">00401099</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esi<BR>0040109A&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ebx<BR>0040109B&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esp,4Ch<BR>0040109E&nbsp;&nbsp;&nbsp;cmp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ebp,esp<BR>004010A0&nbsp;&nbsp;&nbsp;call&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__chkesp&nbsp;(004010c0)<BR>004010A5&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esp,ebp<BR>004010A7&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ebp<BR>004010A8&nbsp;&nbsp;&nbsp;ret</SPAN></DIV><BR>我们主要来看看函数调用部分<BR><BR><STRONG>1.参数压栈</STRONG><BR>push 2<BR>push 1<BR>参数从右向左压栈(__cdcel),esp递减<BR><BR><STRONG>2.调用函数</STRONG><BR>&nbsp;call&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @ILT+5(fun) (0040100a)<BR>这条指令会把下一行代码的地址压栈，也就是函数返回地址。同时跳转到函数入口处<BR><BR><STRONG>3.进入函数体</STRONG><BR>push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ebp<BR>mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ebp,esp<BR>首先保存ebp的地址，然后把esp保存到ebp中去<BR>00401023&nbsp;&nbsp; sub&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; esp,44h<BR>00401026&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ebx<BR>00401027&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; esi<BR>00401028&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; edi<BR>减小stack的指针(注意，stack是从内存的高端向低端生长的)，为局部变量保留一些空间，这里的44h不是固定的，由编译器计算得来<BR>00401029&nbsp;&nbsp; lea&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; edi,[ebp-44h]<BR>0040102C&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ecx,11h<BR>00401031&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eax,0CCCCCCCCh<BR>00401036&nbsp;&nbsp; rep stos&nbsp;&nbsp;&nbsp; dword ptr [edi]<BR>用0xCC填充局部变量空间。这是Debug模式特有的，如果是字符串，你就看到被初始化成"烫烫烫烫烫烫"<BR>至此，整个堆栈变成<BR>|-----------------|<BR>|&nbsp;&nbsp;&nbsp; 局部变量2&nbsp;&nbsp; |<BR>|-----------------|<BR>|&nbsp;&nbsp;&nbsp; 局部变量1&nbsp;&nbsp;&nbsp; |&lt;----ebp-4<BR>|-----------------|<BR>|&nbsp;&nbsp;&nbsp; old ebp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&lt;----ebp<BR>|-----------------|<BR>| 函数返回地址| &lt;----ebp+4<BR>|-----------------|<BR>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 参数1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;| &lt;----ebp+8<BR>|-----------------|<BR>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 参数2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<BR>|-----------------|<BR><BR>Next:<BR>int i = 3;<BR>00401038&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dword ptr [ebp-4],3<BR>这里你看到[ebp-4]就是第一个局部变量i了<BR>0040103F&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eax,dword ptr [ebp+8]<BR>00401042&nbsp;&nbsp; add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eax,dword ptr [ebp+0Ch]<BR>00401045&nbsp;&nbsp; add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eax,dword ptr [ebp-4]<BR>[ebp+8],[ebp+0Ch]分别是a和b了<BR><BR><STRONG>4.函数返回</STRONG><BR>函数的结果都是放在eax中(ps:你可以在vc的watch窗口输入@EAX，就可以直接看到函数返回值了）<BR>00401048&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; edi<BR>00401049&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; esi<BR>0040104A&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ebx<BR>0040104B&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; esp,ebp<BR>0040104D&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ebp<BR>0040104E&nbsp;&nbsp; ret<BR>把edi,esi,ebx恢复，然后恢复esp,ebp,这时函数的返回地址就在栈顶，调用ret就可以返回了。<BR><BR><BR>那如果改变函数的返回地址会怎样？<BR>ok,我们修改一下代码：<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"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">stdio.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;fun2()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">fun2()&nbsp;called</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;fun(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;a,</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;b)<BR>{<BR>&nbsp;&nbsp;&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">3</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">return&nbsp;address:0x%x\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">i</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">fun2&nbsp;address:0x%x\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">fun2);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">int&nbsp;*p&nbsp;=&nbsp;(int*)&amp;fun2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;__asm<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;ebx,p<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;dword&nbsp;ptr[ebp+4],ebx<BR>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">i</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">fun2;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">modify&nbsp;return&nbsp;address</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;a</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">b</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">i;<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;a&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,b</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;result&nbsp;;<BR>&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;fun(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;result;<BR>}</SPAN></DIV><BR>Wow,这时，我们就会发现fun2被调用了。这就是Buffer overrun(缓冲溢出)所做的事情吧。<BR><BR><BR><STRONG>5.最后一步，调用者调整堆栈指针</STRONG><BR>call&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @ILT+5(fun) (0040100a)<BR>&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; esp,8<BR>为什么要调整呢，因为调用之前push两个参数进入栈，现在要恢复它<BR>&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dword ptr [ebp-0Ch],eax<BR>这句话就是享用函数调用的果实了(EAX保存了函数的返回值)<BR><BR>------end--------<BR><BR><BR><img src ="http://www.cppblog.com/sandy/aggbug/1745.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2005-12-14 16:53 <a href="http://www.cppblog.com/sandy/archive/2005/12/14/1745.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>考考你对C++对象模型的理解</title><link>http://www.cppblog.com/sandy/archive/2005/12/06/1557.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Tue, 06 Dec 2005 01:34:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2005/12/06/1557.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/1557.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2005/12/06/1557.html#Feedback</comments><slash:comments>12</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/1557.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/1557.html</trackback:ping><description><![CDATA[这是小明我自编的题目。<BR><BR>1.请在不运行程序的情况下，说出下面的程序运行后会崩溃么？如果是，在哪一行。<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"><SPAN style="COLOR: #008080">&nbsp;1</SPAN>&nbsp;<SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">stdio.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN>&nbsp;<SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">memory.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Test&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN>&nbsp;<SPAN style="COLOR: #000000">{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">));&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;s;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;test()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">test()\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;test1()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">17</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">test1():%d\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">s);&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">19</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">virtual</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;test2()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">test2()\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">22</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">23</SPAN>&nbsp;<SPAN style="COLOR: #000000">};&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">25</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">26</SPAN>&nbsp;<SPAN style="COLOR: #000000">{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">27</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">s;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">28</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">test();&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">29</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">test2();&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">30</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">test1();&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">31</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&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">;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">32</SPAN>&nbsp;<SPAN style="COLOR: #000000">}</SPAN></DIV><BR>2 .修改一下，又是在哪一行呢？<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"><SPAN style="COLOR: #008080">&nbsp;1</SPAN>&nbsp;<SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">stdio.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN>&nbsp;<SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">memory.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Test&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN>&nbsp;<SPAN style="COLOR: #000000">{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">));&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;s;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;test()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">test()\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;test1()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">17</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">test1():%d\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">s);&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">19</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">virtual</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;test2()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">test2()\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">22</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">23</SPAN>&nbsp;<SPAN style="COLOR: #000000">};&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">25</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">26</SPAN>&nbsp;<SPAN style="COLOR: #000000">{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">27</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">s&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Test();&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">28</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">test();&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">29</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">test1();&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">30</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">test2();&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">31</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&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">;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">32</SPAN>&nbsp;<SPAN style="COLOR: #000000">}</SPAN></DIV><BR>3.再修改一下，情况会如何呢？<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"><SPAN style="COLOR: #008080">&nbsp;1</SPAN>&nbsp;<SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">stdio.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN>&nbsp;<SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">memory.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Test&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN>&nbsp;<SPAN style="COLOR: #000000">{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">));&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;s;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;test()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">test()\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;test1()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">17</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">test1():%d\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">s);&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">19</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">virtual</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;test2()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">test2()\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">22</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">23</SPAN>&nbsp;<SPAN style="COLOR: #000000">};&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">25</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">26</SPAN>&nbsp;<SPAN style="COLOR: #000000">{&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">27</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test&nbsp;s&nbsp;;<BR></SPAN><SPAN style="COLOR: #008080">28</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.test();&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">29</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.test1();&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">30</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.test2();&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">31</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&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">;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">32</SPAN>&nbsp;<SPAN style="COLOR: #000000">}</SPAN></DIV>
<P><BR><BR><BR>最后说一下答案吧<BR>第一题是在29行，第二题在30行，最后一题不报错。<BR>如果你不知道为什么，建议看看&lt;&lt;Inside the <a title="C++博客" href="http://www.cppblog.com/sandy" >c++</a> Object Model&gt;&gt;<BR></P><img src ="http://www.cppblog.com/sandy/aggbug/1557.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2005-12-06 09:34 <a href="http://www.cppblog.com/sandy/archive/2005/12/06/1557.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Just for fun:能输出自身代码的程序</title><link>http://www.cppblog.com/sandy/archive/2005/12/02/1483.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Fri, 02 Dec 2005 02:58:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2005/12/02/1483.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/1483.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2005/12/02/1483.html#Feedback</comments><slash:comments>21</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/1483.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/1483.html</trackback:ping><description><![CDATA[<FONT color=#0000ff><STRONG>条件：不借助任何文件操作，输出程序的source code</STRONG></FONT>.<BR><BR><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"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">stdio.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;c[]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;{</SPAN><SPAN style="COLOR: #000000">0x7d</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0xa</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6e</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x74</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6d</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x61</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6e</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x28</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x29</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0xa</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x7b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0xa</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x70</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x72</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6e</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x74</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x66</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x28</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x22</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x23</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6e</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x63</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6c</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x75</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x64</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x65</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3c</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x73</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x74</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x64</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6f</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x2e</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x68</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3e</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x5c</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6e</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x63</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x68</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x61</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x72</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x63</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x5b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x5d</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3d</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x7b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x22</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x29</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0xa</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x66</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6f</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x72</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x28</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6e</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x74</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3d</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x30</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3c</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x73</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x7a</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x65</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6f</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x66</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x28</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x63</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x29</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x2b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x2b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x29</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0xa</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x7b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0xa</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x70</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x72</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6e</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x74</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x66</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x28</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x22</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x30</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x78</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x25</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x78</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x2c</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x22</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x2c</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x63</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x5b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x5d</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x29</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0xa</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x7d</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0xa</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x70</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x72</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x69</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6e</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x74</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x66</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x28</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x22</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x25</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x73</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x22</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x2c</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x63</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x29</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0xa</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x72</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x65</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x74</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x75</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x72</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x6e</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x30</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x3b</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0xa</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0x7d</SPAN><SPAN style="COLOR: #000000">,};<BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">#include&nbsp;&lt;stdio.h&gt;\nchar&nbsp;c[]&nbsp;=&nbsp;{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(</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: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(c);</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">i)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">0x%x,</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,c[i]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">%s</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,c);<BR>&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></DIV><BR>至于怎么做到的，大家可以自己琢磨<BR><BR>不过不要以为只有C能做到，java也可以哦<BR><BR><BR><BR><img src ="http://www.cppblog.com/sandy/aggbug/1483.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2005-12-02 10:58 <a href="http://www.cppblog.com/sandy/archive/2005/12/02/1483.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>