首页 > 代码库 > 断言assert的应用

断言assert的应用

断言的应用

1
#include <stdio.h> 2 //disable the assert function if "NDEBUG" defined 3 //#define NDEBUG 4 #include <assert.h> 5 6 void print_number(int* myInt) { 7 assert (myInt!=NULL); 8 printf ("%d\n",*myInt); 9 } 10 11 int main (int argc, char* argv[]) 12 { 13 int a=10; 14 int * b = NULL; 15 int * c = NULL; 16 17 b=&a; 18 //comment this line to generate an assert 19 //c=&argc; 20 21 print_number (b); 22 print_number (c); 23 24 return 0; 25 }

输出结果举例:

  10
  Assertion failed: myInt!=NULL, file C:\_Tasks\_VC6.0\Temp\Hello.cpp, line 7
  Press any key to continue