Loading

C语言assert()函数用法总结

C语言assert()函数用法总结

        </h1>
        <div class="clear"></div>
        <div class="postBody">

assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:

#include <assert.h>
void assert( int expression );

assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。请看下面的程序清单badptr.c:

复制代码
 1 #include <stdio.h>
 2 #include <assert.h>
 3 #include <stdlib.h>
 4 int main( void )
 5 {
 6        FILE *fp;
 7     
 8        fp = fopen( "test.txt", "w" );//以可写的方式打开一个文件,如果不存在就创建一个同名文件
 9        assert( fp );                           //所以这里不会出错
10        fclose( fp );
11     
12        fp = fopen( "noexitfile.txt", "r" );//以只读的方式打开一个文件,如果不存在就打开文件失败
13        assert( fp );                           //所以这里出错
14        fclose( fp );                           //程序永远都执行不到这里来
15        return 0;
16 }
复制代码

已放弃使用assert()的缺点是,频繁的调用会极大的影响程序的性能,增加额外的开销。在调试结束后,可以通过在包含#include <assert.h>的语句之前插入 #define NDEBUG 来禁用assert调用,示例代码如下:

#include <stdio.h>
#define NDEBUG
#include <assert.h>

用法总结与注意事项:

1)在函数开始处检验传入参数的合法性如:

复制代码
int resetBufferSize(int nNewSize)
{
  //功能:改变缓冲区大小,
  //参数:nNewSize 缓冲区新长度
  //返回值:缓冲区当前长度 
  //说明:保持原信息内容不变     nNewSize<=0表示清除缓冲区
  assert(nNewSize >= 0);
  assert(nNewSize <= MAX_BUFFER_SIZE);
  ...
}
复制代码

  2)每个assert只检验一个条件,因为同时检验多个条件时,如果断言失败,无法直观的判断是哪个条件失败,如:

 不好:

assert(nOffset>=0 && nOffset+nSize<=m_nInfomationSize);

 好:

assert(nOffset >= 0);
assert(nOffset+nSize <= m_nInfomationSize);
3)不能使用改变环境的语句,因为assert只在DEBUG个生效,如果这么做,会使用程序在真正运行时遇到问题,如:

 错误:

assert(i++ < 100);

这是因为如果出错,比如在执行之前i=100,那么这条语句就不会执行,那么i++这条命令就没有执行。

 正确:

assert(i < 100);
 i++;

   4)assert和后面的语句应空一行,以形成逻辑和视觉上的一致感。
  5)有的地方,assert不能代替条件过滤。

<div id="blog_post_info">
    </div>
</div>
<div class="clear"></div>
<div id="author_profile_honor"></div>
<div id="author_profile_follow">
            
</div>
<div class="clear"></div>
<div id="post_next_prev">

    </div>
    
    
</div><!--end: topics 文章、评论容器-->
<div id="comment_form_container" style="visibility: visible;"></div>
<div class="ad_text_commentbox" id="ad_text_under_commentbox"></div>

<div id="opt_under_post"></div>
<script async="async" src="https://www.googletagservices.com/tag/js/gpt.js"></script>
<script>
    var googletag = googletag || {};
    googletag.cmd = googletag.cmd || [];
</script>
<script>
    googletag.cmd.push(function () {
        googletag.defineSlot("/1090369/C1", [300, 250], "div-gpt-ad-1546353474406-0").addService(googletag.pubads());
        googletag.defineSlot("/1090369/C2", [468, 60], "div-gpt-ad-1539008685004-0").addService(googletag.pubads());
        googletag.pubads().enableSingleRequest();
        googletag.enableServices();
    });
</script>
<div id="cnblogs_c1" class="c_ad_block" style="">
    <div id="div-gpt-ad-1546353474406-0" style="height: 250px; width: 300px;"><div id="google_ads_iframe_/1090369/C1_0__container__" style="border: 0pt none; width: 300px; height: 250px;"></div></div>
</div>
<div id="under_post_news"></div>
<div id="cnblogs_c2" class="c_ad_block" style="">
    <div id="div-gpt-ad-1539008685004-0" style="height: 60px; width: 468px;">
        <script>
            if (new Date() >= new Date(2018, 9, 13)) {
                googletag.cmd.push(function () { googletag.display("div-gpt-ad-1539008685004-0"); });
            }
        </script>
    <div id="google_ads_iframe_/1090369/C2_0__container__" style="border: 0pt none; width: 468px; height: 60px;"></div></div>
</div>
<div id="under_post_kb">
posted @ 2020-03-06 15:53  Lowell_liu  阅读(473)  评论(0编辑  收藏  举报