loading...
  1. Usage of “Assert” in OpenCV World

Usage of “Assert” in OpenCV World

Hello Folks,

Today, we’ll learn about the most interesting ASSERT function in OpenCV.

assert

 will terminate the program (usually with a message quoting the assert statement) if its argument turns out to be false. it’s commonly used during debugging to make the program fail more obviously if an unexpected condition occurs.

assert(length >= 0); // die if length is negative.
You can also add a more informative message to be displayed if it fails like so:

for example:

assert(length >= 0); // die if length is negative.
You can also add a more informative message to be displayed if it fails like so:

assert(length >= 0 && “Whoops, length can’t possibly be negative! (didn’t we just check 10 lines ago?) Tell jsmith”);

Or else like this:
assert((“Length can’t possibly be negative! Tell jsmith”, length >= 0));

When you’re doing a release (non-debug) build, you can also remove the overhead of evaluating assert statements by defining the NDEBUG macro, usually with a compiler switch. The corollary of this is that your program should never rely on the assert macro running.

// 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);

From the combination of the program calling abort() and not being guaranteed to do anything, asserts should only be used to test things that the developer has assumed rather than, for example, the user entering a number rather than a letter (which should be handled by other means).

Modern C++ libraries for your OpenCV toolbox
How to detect a Christmas Tree using C++?




Leave a Reply

Your email address will not be published. Required fields are marked *

Welcome to OpenCV World !! Come as a Guest, stay as a Family