zoukankan      html  css  js  c++  java
  • assert断言

    https://www.cnblogs.com/ggzss/archive/2011/08/18/2145017.html

    assert是一个宏定义,其作用是如果它的条件返回错误,则终止程序执行,原型定义:

    1 #include <assert.h>
    2 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        fp = fopen( "test.txt", "w" );//以可写的方式打开一个文件,如果不存在就创建一个同名文件       
     8       assert( fp );                           //所以这里不会出错       fclose( fp );           
     9         fp = fopen( "noexitfile.txt", "r" );//以只读的方式打开一个文件,如果不存在就打开文件失败       
    10        assert( fp );                           //所以这里出错       
    11        fclose( fp );                           //程序永远都执行不到这里来               
    12        return 0;
    13 }

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

    1 #include <stdio.h>
    2 #define NDEBUG
    3 #include <assert.h>
  • 相关阅读:
    MySQL-keepalived做高可用
    Linux-服务管理
    MySQL-CentOS7上安装Mysql5.7
    MySQL-查看DB文件位置
    游戏编程与游戏种类
    计算机
    python
    python中的构造函数
    IndentationError:expected an indented block错误解决
    python程序的pdb调试方法
  • 原文地址:https://www.cnblogs.com/linhaostudy/p/8039905.html
Copyright © 2011-2022 走看看