Easy Tutorial
❮ Java Annotation C Language Pointer From Five Dimensions ❯

Usage of Assert (assert)

Category Programming Techniques

I always thought assert was just an error reporting function. In fact, it turns out to be a macro, and its purpose is not merely to "report errors."

After gaining some understanding of it, I've come to see assert() as a form of "contract programming." In my understanding, it expresses the idea that the program operates normally and well under my assumed conditions. It's essentially equivalent to an if statement:

if(assumption holds)
{
    program runs normally;
}
else
{
    report error && terminate program! (to prevent larger errors from program execution)
}

However, writing it this way would lead to countless if statements, and sometimes an if statement's brackets could span from the beginning to the end of a file. Moreover, most of the assumptions we need to validate are occasional events, or we simply want to test if some worst-case scenarios occur. This is where assert() comes in.

The assert macro is defined in assert.h, and its function is to terminate the program execution if its condition returns an error.

#include "assert.h"
void assert(int expression);

The assert function first evaluates the expression. If its value is false (i.e., 0), it prints an error message to stderr and then aborts the program by calling abort.

The drawback of using assert is that frequent calls can significantly impact the program's performance, adding extra overhead.

After debugging, you can disable assert calls by inserting #define NDEBUG before the #include statement, as shown below:

#include
#define NDEBUG
#include

Summary and Notes on Usage

1) Validate the legality of input parameters at the beginning of the function

Example:

int resetBufferSize(int nNewSize)
{
// Function: Change buffer size
// Parameter: nNewSize - new buffer length
// Return value: current buffer length
// Note: Preserve original information content; nNewSize <= 0 means clearing the buffer
assert(nNewSize >= 0);
assert(nNewSize <= MAX_BUFFER_SIZE);

...
}

2) Each assert should check only one condition, as failing multiple conditions simultaneously makes it unclear which one failed

Not recommended:

assert(nOffset >= 0 && nOffset + nSize <= m_nInfomationSize);

Recommended:

assert(nOffset >= 0);
assert(nOffset + nSize <= m_nInfomationSize);

3) Do not use statements that alter the environment, as assert only works in DEBUG mode and can cause issues in real-world execution

Incorrect: assert(i++ < 100)

This is because if an error occurs, for example, if i = 100 before execution, this statement won't execute, and the i++ command won't be executed.

Correct:

assert(i < 100)
i++;

4) Leave a blank line between assert and the following statement for logical and visual consistency

5) Assert cannot replace conditional filtering

Programs are generally divided into Debug and Release versions, with the Debug version used for internal debugging and the Release version for user deployment. The assert macro only works in the Debug version and is used to check conditions that should not occur. The following is a memory copying program. If the assert parameter is false during runtime, the program will terminate (usually with a prompt indicating where the assert was triggered).

Principles for using asserts:

ASSERT() is a macro commonly used during program debugging. It evaluates the expression within parentheses during program runtime. If the expression is FALSE (0), the program reports an error and terminates execution. If the expression is not 0, it proceeds to execute the subsequent statements. This macro is typically used to identify clearly illegal data in the program, terminating the program to prevent serious consequences and facilitate error finding.

ASSERT only works in the Debug version and is ignored when compiled as a Release version.

Original Source: https://www.cnblogs.com/thisway/p/5558914.html

** Click to Share Notes

Cancel

-

-

-

❮ Java Annotation C Language Pointer From Five Dimensions ❯