我一直在学习OpenCV教程,遇到了assert函数;它能做什么?


当前回答

看一看

assert() c++示例程序

Many compilers offer an assert() macro. The assert() macro returns TRUE if its parameter evaluates TRUE and takes some kind of action if it evaluates FALSE. Many compilers will abort the program on an assert() that fails; others will throw an exception One powerful feature of the assert() macro is that the preprocessor collapses it into no code at all if DEBUG is not defined. It is a great help during development, and when the final product ships there is no performance penalty nor increase in the size of the executable version of the program.

Eg

#include <stdio.h>
#include <assert.h>

void analyze (char *, int);

int main(void)
{
   char *string = "ABC";
   int length = 3;

   analyze(string, length);
   printf("The string %s is not null or empty, "
          "and has length %d \n", string, length);
}

void analyze(char *string, int length)
{
   assert(string != NULL);     /* cannot be NULL */
   assert(*string != '\0');    /* cannot be empty */
   assert(length > 0);         /* must be positive */
}

/****************  Output should be similar to  ******************
The string ABC is not null or empty, and has length 3

其他回答

assert computer语句类似于英语中的make sure语句。

使用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.

此外,您还可以使用它来检查动态分配是否成功。

代码示例:

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语句抛出异常吗?)

下面是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位边界上对齐的,因此编译器可以生成针对这种对齐的特定指令。

它是一个函数,如果它计算的值为false,则将停止程序执行。通常它被宏包围,这样在使用发布设置编译时就不会编译到结果二进制文件中。

它被设计用来测试你所做的假设。例如:

void strcpy(char* dest, char* src){
    //pointers shouldn't be null
    assert(dest!=null);
    assert(src!=null);

    //copy string
    while(*dest++ = *src++);
}

理想的情况是,您可以在程序中犯错误,例如调用带有无效参数的函数,并且在它发生段错误(或未能按预期工作)之前命中断言。