micro assert 宏assert()

Posted diamondDemand

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了micro assert 宏assert()相关的知识,希望对你有一定的参考价值。

assert macro
Note: assert is a macro, not a function.in the c assert.h header file. the prototype of an assert macro is defined in <assert.h> and its effect is that if its condition return an false, the program execution is terminated, the prototype is defined:
#defineassert(expr)\
((expr)\
?__ASSERT_VOID_CAST(0)\
:__assert_fail(__STRING(expr),__FILE__,__LINE__,__ASSERT_FUNCTION))

/*DefinedInGlibc2.15*/ glibc is the libary which is issued by GNU
the role of assert is to calculate the expression, if its value is false(that is zero), then is will print out the contents of assert and __FILE__, __LINE,__ASSERT_FUNCTION, and then abort() function make kernel kill its coredump(whether to generate coredump files, depending on the system configuration);otherwhise,assert() has no effect, Macro assert() is generally used to confirm the normal operation of the program, the assert is true when the expression is true. after debug is complete, you do not have to move the assert() statement form the source code because the macro assert is defined as NULL when the macro NDEBUG is defined


它的作用是先计算表达式expr,如果其值为假(即为0),那么它会打印出来assert的内容和__FILE__, __LINE__, __ASSERT_FUNCTION,然后执行abort()函数使kernel杀掉自己并coredump(是否生成coredump文件,取决于系统配置);否则,assert()无任何作用。宏assert()一般用于确认程序的正常操作,其中表达式构造无错时才为真值。完成调试后,不必从源代码中删除assert()语句,因为宏NDEBUG有定义时,宏assert()的定义为空。[1]

quote from baidu encyclopedia
引用自百度百科

以上是关于micro assert 宏assert()的主要内容,如果未能解决你的问题,请参考以下文章

宏定义的黑魔法——assert原理详解

如何用宏做static_assert?

assert.strictEqual()

assert.equal()

ASSERT_VALID(pDoc)分析

OpenCV中的带参数宏定义CV_Assert()的作用