zoukankan      html  css  js  c++  java
  • assert的用处

    ASSERT函数是用于调试中,也就是说在你的代码中当是Debug的时候
    它完成对参数的判断,如果是TRUE则什么都不做,如果是FALSE则
    弹出一个程序中断对话框提示程序出现错误。
    在Release版本中它是什么作用都不起。

    它主要是监视程序在调试运行的过程中的运行情况,
    多多使用它,绝对有好处,没有一点坏处。

    例如:

    /* ASSERT.C: In this program, the analyze_string function uses
     * the assert function to test several conditions related to
     * string and length. If any of the conditions fails, the program
     * prints a message indicating what caused the failure.
     */
    
    #include <stdio.h>
    #include <assert.h>
    #include <string.h>
    
    void analyze_string( char *string );   /* Prototype */
    
    void main( void )
    {
       char  test1[] = "abc", *test2 = NULL, test3[] = "";
    
       printf ( "Analyzing string '%s'
    ", test1 );
       analyze_string( test1 );
       printf ( "Analyzing string '%s'
    ", test2 );
       analyze_string( test2 );
       printf ( "Analyzing string '%s'
    ", test3 );
       analyze_string( test3 );
    }
    
    /* Tests a string to see if it is NULL, */ 
    /*   empty, or longer than 0 characters */
    void analyze_string( char * string )
    {
       assert( string != NULL );        /* Cannot be NULL */
       assert( *string != '' );       /* Cannot be empty */
       assert( strlen( string ) > 2 );  /* Length must exceed 2 */
    }
    
    Output
    Analyzing string 'abc'
    Analyzing string '(null)'
    Assertion failed: string != NULL, file assert.c, line 24
    
    abnormal program termination
  • 相关阅读:
    数据库
    计算机基础知识系列
    《大话数据结构》参考
    数据结构与算法系列
    python cookbook
    Python教程 廖雪峰
    Python入门学习系列
    认识 React——声明式,高效且灵活的用于构建用户界面的 JavaScript 库
    线程---同步(synchronized)
    线程---插队和礼让执行(join和yield)
  • 原文地址:https://www.cnblogs.com/zhanbiqiang/p/4010352.html
Copyright © 2011-2022 走看看