我一直在学习OpenCV教程,遇到了assert函数;它能做什么?
当前回答
像“抛出异常”和“停止执行”这样的东西可能对大多数编译器是正确的,但不是所有的。(顺便问一下,真的有assert语句抛出异常吗?)
下面是c6x和其他TI编译器使用的assert的一个有趣的、略有不同的含义:在看到某些assert语句时,这些编译器使用该语句中的信息来执行某些优化。邪恶。
C语言示例:
int dot_product(short *x, short *y, short z)
{
int sum = 0
int i;
assert( ( (int)(x) & 0x3 ) == 0 );
assert( ( (int)(y) & 0x3 ) == 0 );
for( i = 0 ; i < z ; ++i )
sum += x[ i ] * y[ i ];
return sum;
}
这告诉解编译器数组是在32位边界上对齐的,因此编译器可以生成针对这种对齐的特定指令。
其他回答
c++ 11 N3337标准草案
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
19.3断言
头文件<cassert>,如表42所示,提供了一个宏,用于记录c++程序断言 以及禁用断言检查的机制。 2内容与标准C库头文件<assert.h>相同。
c99n1256标准草案
http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf
7.2诊断<assert.h> .h
1 The header <assert.h> defines the assert macro and refers to another macro, NDEBUG which is not defined by <assert.h>. If NDEBUG is defined as a macro name at the point in the source file where <assert.h> is included, the assert macro is defined simply as #define assert(ignore) ((void)0) The assert macro is redefined according to the current state of NDEBUG each time that <assert.h> is included. 2. The assert macro shall be implemented as a macro, not as an actual function. If the macro definition is suppressed in order to access an actual function, the behavior is undefined.
7.2.1程序诊断
7.2.1.1 assert宏
Synopsis 1. #include <assert.h> void assert(scalar expression); Description 2 The assert macro puts diagnostic tests into programs; it expands to a void expression. When it is executed, if expression (which shall have a scalar type) is false (that is, compares equal to 0), the assert macro writes information about the particular call that failed (including the text of the argument, the name of the source file, the source line number, and the name of the enclosing function — the latter are respectively the values of the preprocessing macros __FILE__ and __LINE__ and of the identifier __func__) on the standard error stream in an implementation-defined format. 165) It then calls the abort function. Returns 3 The assert macro returns no value.
它是一个函数,如果它计算的值为false,则将停止程序执行。通常它被宏包围,这样在使用发布设置编译时就不会编译到结果二进制文件中。
它被设计用来测试你所做的假设。例如:
void strcpy(char* dest, char* src){
//pointers shouldn't be null
assert(dest!=null);
assert(src!=null);
//copy string
while(*dest++ = *src++);
}
理想的情况是,您可以在程序中犯错误,例如调用带有无效参数的函数,并且在它发生段错误(或未能按预期工作)之前命中断言。
如果Assert的参数为假,Assert将终止程序(通常使用引用Assert语句的消息)。它通常在调试期间使用,以使程序在发生意外情况时更明显地失败。
例如:
assert(length >= 0); // die if length is negative.
你也可以添加一个信息更丰富的消息显示,如果它失败了,如下所示:
assert(length >= 0 && "Whoops, length can't possibly be negative! (didn't we just check 10 lines ago?) Tell jsmith");
或者像这样:
assert(("Length can't possibly be negative! Tell jsmith", length >= 0));
在进行发布(非调试)构建时,还可以通过定义NDEBUG宏(通常带有编译器开关)来消除求值assert语句的开销。这样做的必然结果是,您的程序永远不应该依赖assert宏运行。
// BAD
assert(x++);
// GOOD
assert(x);
x++;
// Watch out! Depends on the function:
assert(foo());
// Here's a safer way:
int ret = foo();
assert(ret);
由于程序调用abort()并且不能保证执行任何操作,因此断言应该只用于测试开发人员假设的事情,而不是用于测试用户输入的数字而不是字母(这应该通过其他方式处理)。
此外,您还可以使用它来检查动态分配是否成功。
代码示例:
int ** p;
p = new int * [5]; // Dynamic array (size 5) of pointers to int
for (int i = 0; i < 5; ++i) {
p[i] = new int[3]; // Each i(ptr) is now pointing to a dynamic
// array (size 3) of actual int values
}
assert (p); // Check the dynamic allocation.
类似于:
if (p == NULL) {
cout << "dynamic allocation failed" << endl;
exit(1);
}
使用assert()函数而不是普通的if else和printf有三个主要原因
assert() function is mainly used in the debugging phase, it is tedious to write if else with a printf statement everytime you want to test a condition which might not even make its way in the final code. In large software deployments , assert comes very handy where you can make the compiler ignore the assert statements using the NDEBUG macro defined before linking the header file for assert() function. assert() comes handy when you are designing a function or some code and want to get an idea as to what limits the code will and not work and finally include an if else for evaluating it basically playing with assumptions.