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

📄 使用 mfc的 assertions.htm

📁 VC的一些技巧性文档
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>Using MFC Assertions</title><!-- Author: Gordon Mueller --></head><body aLink="#990033" background="/images/navigation/a/bg.gif" bgColor="#211063"leftMargin="0" link="#990033" text="#000000" topMargin="0" vLink="#990033"><!-- FC HP BN1 START --><!-- Please do not remove the line above or the code within this section --><!-- Doing so may cause damage to your pages or insertion of multiple banners--><center><p><table border="0" cellspacing="0" cellpadding="0"><tr><td colspan="11"><img src="http://www.fortunecity.com/console2/newnav/top.gif" height="7" width="555"></td></tr><tr><td><img src="http://www.fortunecity.com/console2/newnav/left.gif" alt="FC Navigation Console" height="60" width="72" border="0" usemap="#fcleft"><map name="fcleft"><area shape="rect" coords="10,43,64,60" href="http://www2.fortunecity.com/cgi-bin/homepage/estate.pl?referer=navbar" target="_top"><area shape="rect" coords="9,21,61,36" href="http://www2.fortunecity.com/cgi-bin/showarea.pl?area=skyscraper&referer=navbar" target="_top"><area shape="rect" coords="8,0,62,15" href="http://www.fortunecity.com" target="_top"><area shape="rect" href="http://www.fortunecity.com" target="_top" coords="0,0,49,49"></map></td><td colspan="9"><A HREF="http://ad.doubleclick.net/jump/fc.us468/member/tech;s1=m;s3=tech;pos=1;tag=g;sz=468x60;mtile=1;num=32946?"><IMG SRC="http://ad.doubleclick.net/ad/fc.us468/member/tech;s1=m;s3=tech;pos=1;tag=g;sz=468x60;mtile=1;num=32946?" border=0 height="60" width="468"></A></td><td><img src="http://www.fortunecity.com/console2/newnav/right.gif" width="15" height="60"></td></tr><tr><td><img src="http://www.fortunecity.com/console2/newnav/left2.gif" height="31" width="72" target="_top"></td><td><img src="http://www.fortunecity.com/console2/newnav/gap1.gif" width="17" height="31"></td><td><a href="http://adex3.flycast.com/server/socket/127.0.0.1:2800/click/FortuneCitycom/NavBar1/32946" target="_top"><img src="http://adex3.flycast.com/server/socket/127.0.0.1:2800/ad/FortuneCitycom/NavBar1/32946" border="0" width="88" height="31"></a></td><td><img src="http://www.fortunecity.com/console2/newnav/gap2.gif" width="24" height="31"></td><td><a href="http://adex3.flycast.com/server/socket/127.0.0.1:2800/click/FortuneCitycom/NavBar2/32946" target="_top"><img src="http://adex3.flycast.com/server/socket/127.0.0.1:2800/ad/FortuneCitycom/NavBar2/32946" border="0" width="88" height="31"></a></td><td><img src="http://www.fortunecity.com/console2/newnav/gap3.gif" width="24" height="31"></td><td><a href="http://adex3.flycast.com/server/socket/127.0.0.1:2800/click/FortuneCitycom/NavBar3/32946" target="_top"><img src="http://adex3.flycast.com/server/socket/127.0.0.1:2800/ad/FortuneCitycom/NavBar3/32946" border="0" width="88" height="31"></a></td><td><img src="http://www.fortunecity.com/console2/newnav/gap4.gif" width="19" height="31"></td><td><a href="http://www2.fortunecity.com/cgi-bin/homepage/navbarforward.cgi?from=nav4&referer=navbare" target="_top"><img src="http://www.fortunecity.com/console2/newnav/button4.gif" border="0" width="88" height="31"></a></td><td><img src="http://www.fortunecity.com/console2/newnav/gap5.gif" width="32" height="31"></td><td><img src="http://www.fortunecity.com/console2/newnav/right2.gif" width="15" height="31"></td></tr></table></p></center><!-- WARNING: under no circumstances remove the line below.  Doing so may damage your page.--><!-- FC HP BN1 END --><!--- top table - advertising ------------------------------------------------------><!---end top table - advertising --------------------------------------------------><!--- main table ------------------------------------------------------------------><table border="0" cellPadding="0" cellSpacing="0" width="100%"><TBODY>  <tr>    <td bgColor="#000000"><img height="1" src="/images/space.gif" width="135"></td>    <td bgColor="#000000"><img height="1" src="/images/space.gif" width="10"></td>  </tr>  <tr>    <td bgColor="#ffffff" colSpan="2" vAlign="top" width="99%"><!--- content table ---------------------------------------------------------> <table bgColor="#ffffff"    border="0" cellPadding="10" cellSpacing="0" width="100%"><TBODY>      <tr>        <td><font face="arial,helvetica" style="FONT-SIZE: 12px" ;><h1></font><font        color="#990033">使用 MFC的 Assertions</font><font></h1>        <p><font face="arial,helvetica" style="FONT-SIZE: 12px" ;>By Dave Schmitt</font> </p>        <p></font><font style="FONT-SIZE: 12px" ;>明智地使用</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>assertions</font><font        style="FONT-SIZE: 12px" ;>、</font><font face="arial,helvetica" style="FONT-SIZE: 12px" ;>trace</font><font        style="FONT-SIZE: 12px" ;>消息和其它编译工具对于调试程序是很重要的。</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>Microsoft</font><font        style="FONT-SIZE: 12px" ;>清醒地意识到对于一个复杂的编程库来说潜在的技术支持是一个巨大的负担,因此他们在整个的</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>MFC</font><font style="FONT-SIZE: 12px" ;>代码中到处放置了调试语句。</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;></p>        <p></font><font style="FONT-SIZE: 12px" ;>我们也可以在自己的应用程序中模仿这种好习惯,于是我决定将我的精力集中在下面的</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>MFC</font><font style="FONT-SIZE: 12px" ;>调试主题上:</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>assertions</font><font        style="FONT-SIZE: 12px" ;>,</font><font face="arial,helvetica" style="FONT-SIZE: 12px" ;>trace</font><font        style="FONT-SIZE: 12px" ;>消息,运行时信息(</font><font face="arial,helvetica"        style="FONT-SIZE: 12px" ;>RTTI</font><font style="FONT-SIZE: 12px" ;>),例外,数据倾倒,内存漏洞检测和内存破坏检测。本月我将研究</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>MFC</font><font style="FONT-SIZE: 12px" ;>的</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>assertion</font><font        style="FONT-SIZE: 12px" ;>功能。</font><font face="arial,helvetica"        style="FONT-SIZE: 12px" ;></p>        <p><font color="#990033" size="+1"><b>Assertions</b></font> </p>        <p></font><font style="FONT-SIZE: 12px" ;>举个例子,假设你设计了一个函数,该函数需要一个指向文档对象的指针做参数,但是我错误地用一个视指针调用了你的这个函数。你使用这个假的地址将导致视数据的破坏,如果我们幸运的话,这个程序则仅仅是偏离了该函数作用的轨道。然而,问题不会仅仅影响它本身,当我在接下来的程序中使用被破坏的视数据时,问题更大了。我将不得不做大量的检测工作来发现这个最初的指针错误。</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;></p>        <p></font><font style="FONT-SIZE: 12px" ;>这种类型的问题可以被完全避免,只要你在该函数的开始处实现一个</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>assert</font><font        style="FONT-SIZE: 12px" ;>测试,用来检测该指针是否真正指向一个文档对象。好的编程人员例如</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>MFC</font><font style="FONT-SIZE: 12px" ;>的设计者都会在每个函数的开始处例行公事地使用</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>assertion</font><font        style="FONT-SIZE: 12px" ;>。毕竟,你无法知道谁将</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>call</font><font style="FONT-SIZE: 12px"        ;>你。</font><font face="arial,helvetica" style="FONT-SIZE: 12px" ;></p>        <p></font><b><font color="#990033" size="+1" face="arial,helvetica"        style="FONT-SIZE: 12px" ;>ASSERT</font><font color="#990033" style="FONT-SIZE: 12px" ;        size="5">宏</font></b><font style="FONT-SIZE: 12px" ;><big><big><big><big> </big></big></big></big></font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;></p>        <p>assert</font><font style="FONT-SIZE: 12px" ;>宏将判断表达式。如果一个表达式为真,执行将继续。否则,程序将显示一条消息并且暂停。你可以选择忽视这条错误并继续、终止这个程序或者是跳到调试器中。这里是一个例子,它演示了如何使用一个</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>ASSERT</font><font        style="FONT-SIZE: 12px" ;>宏去验证一个语句。</font><font face="arial,helvetica"        style="FONT-SIZE: 12px" ;></p>        <table cellPadding="5" width="100%"><TBODY>          <tr>            <td bgColor="#ccccff"><pre>void foo(char* p,int size){ASSERT(p != 0);			// make sure buffer pointer is supplied</font><fontstyle="FONT-SIZE: 12px" ;>确认缓冲区的指针是有效的</font><font face="arial,helvetica"style="FONT-SIZE: 12px" ;>ASSERT((size &gt;= 100);		// make sure buffer is at least 100 bytes</font><fontstyle="FONT-SIZE: 12px" ;>确认缓冲区至少有</font><font face="arial,helvetica"style="FONT-SIZE: 12px" ;>100</font><font style="FONT-SIZE: 12px" ;>个字节<fontstyle="FONT-SIZE: 12px" ;></font></font><font face="arial,helvetica" style="FONT-SIZE: 12px" ;><fontface="arial,helvetica" style="FONT-SIZE: 12px" ;>// Do the foo calculation}</font></pre>            </font></td>          </tr></TBODY>        </table>        <p><font style="FONT-SIZE: 12px" ;>这些语句不产生任何代码,除非</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>_DEBUG</font><font        style="FONT-SIZE: 12px" ;>处理器标志被设置。</font><font face="arial,helvetica"        style="FONT-SIZE: 12px" ;>Visual C++</font><font style="FONT-SIZE: 12px" ;>只在调试版本设置这些标志,而在发行版本不定义这些标志。当</font><font        face="arial,helvetica" style="FONT-SIZE: 12px" ;>_DEBUG</font><font        style="FONT-SIZE: 12px" ;>被定义时,两个</font><font face="arial,helvetica"        style="FONT-SIZE: 12px" ;>assertions</font><font style="FONT-SIZE: 12px" ;>将产生如下代码:</font><font

⌨️ 快捷键说明

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