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
原文地址:https://www.cnblogs.com/zhanbiqiang/p/4010352.html