assert宏

/* assert example */
#include <stdio.h>      /* printf */
#include <assert.h>     /* assert */

void print_number(int* myInt) {
  assert (myInt!=NULL);
  printf ("%d\n",*myInt);
}

int main ()
{
  int a=10;
  int * b = NULL;
  int * c = NULL;

  b=&a;

  print_number (b);
  print_number (c);

  return 0;
}

执行结果:

[root workspace]#gcc -g -o assert assert.c
[root workspace]#./assert
10
assert: assert.c:6: print_number: Assertion `myInt!=((void *)0)' failed.
已放弃(吐核)

如果想在发布版本中禁用assert,可以使用如下两种方式:
编译选项添加NDEBUG
包含assert.h头文件前定义宏NDEBUG
执行结果:

[root workspace]#gcc -g -DNDEBUG -o assert assert.c
[root workspace]#./assert
10
段错误(吐核)