assert() : 如何正确的使用

请先阅读一下man page,不要急躁。

ASSERT(3)                            Linux Programmer's Manual                           ASSERT(3)

NAME
       assert - abort the program if assertion is false

SYNOPSIS
       #include <assert.h>

       void assert(scalar expression);

DESCRIPTION
       If  the  macro  NDEBUG  was  defined  at the moment <assert.h> was last included, the macro
       assert() generates no code, and hence does nothing at all.  Otherwise, the  macro  assert()
       prints an error message to standard error and terminates the program by calling abort(3) if
       expression is false (i.e., compares equal to zero).

       The purpose of this macro is to help programmers find bugs in their programs.  The  message
       "assertion  failed  in  file foo.c, function do_bar(), line 1287" is of no help at all to a
       user.

RETURN VALUE
       No value is returned.

ATTRIBUTES
       For an explanation of the terms used in this section, see attributes(7).

       ┌──────────┬───────────────┬─────────┐
       │Interface │ Attribute     │ Value   │
       ├──────────┼───────────────┼─────────┤
       │assert()  │ Thread safety │ MT-Safe │
       └──────────┴───────────────┴─────────┘

CONFORMING TO
       POSIX.1-2001, POSIX.1-2008, C89, C99.  In C89, expression is required to be of type int and
       undefined behavior results if it is not, but in C99 it may have any scalar type.

BUGS
       assert()  is  implemented  as  a  macro; if the expression tested has side-effects, program
       behavior will be different depending on whether NDEBUG is defined.  This may create Heisen‐
       bugs which go away when debugging is turned on.

SEE ALSO
       abort(3), assert_perror(3), exit(3)

COLOPHON
       This  page  is  part  of release 4.04 of the Linux man-pages project.  A description of the
       project, information about reporting bugs, and the latest version  of  this  page,  can  be
       found at http://www.kernel.org/doc/man-pages/.

GNU                                         2015-08-08                                   ASSERT(3)

如果你没有耐心,请从这里看,希望你不是。

几个关键的问题:

  1. 什么时候使用assert

    assert通常用于诊断程序中潜在的BUG,通过使用assert(condition), 当condition为false时,程序提前结束运行,利于程序BUG的定位。所以,当你觉得某个condition必须为true,但是某些条件下有可能为false时,可以使用assert来强化语义。(有时程序的逻辑很复杂,你也不不确定condition何时为false,所以告诉程序如果condition为false了请结束掉程序,并打印相关信息,以便帮助自己梳理逻辑,找到BUG)

  2. 使用assert时需要注意什么

    assert本身也是个宏定义,它宏展开结果与宏NDEBUG有密切的联系。如果NDEBUG在头文件<assert.h>前定义了,那么assert宏展开后什么都没有了(灰飞烟灭)。

# define __ASSERT_VOID_CAST (void)

/* void assert (int expression);

   If NDEBUG is defined, do nothing.
   If not, and EXPRESSION is zero, print an error message and abort.  */

#ifdef  NDEBUG

# define assert(expr)       (__ASSERT_VOID_CAST (0))
...
#else
...
# define assert(expr)                           \
  ((expr)                               \
   ? __ASSERT_VOID_CAST (0)                     \
   : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))

#endif

所以你如果在assert放入了某个函数,这将产生一个隐藏很深的BUG,>_<

assert(StartWork() == true);

NDEBUG没有定义时StartWorker会被调用,但是一旦某个地方定义了NDEBUG(鬼知道在哪里谁定义的),StartWork就被雪藏了,永远无法上场。一种改进措施是只将返回值放入assert,函数以独立语句放置:

int r = StartWork();
assert(r == true);

猜你喜欢

转载自blog.csdn.net/chj90220/article/details/80060264
今日推荐