Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated assert NCCE as per David's feedback

...

In this example, a programmer attempts to access their own verification functionality by suppressing the assert macro and instead sending control to an assert function.

Suppose the custom <myassert.h> declares a function assert which does nonstandard verification, and the standard <assert.h> defines an assert macro as required by the standard.

Code Block
bgColor#FFcccc
#include <assert<myassert.h>

// suppose the following is the definition of the assert()
// macro in <assert.h> and there is no function with the
// same name:
// #define assert(expr)   \
//   ((expr) ? (void)0 \
//           : (void)fprintf(stderr, "Assertion failed: %s:%d (%s): %s\n", \
//                            __FILE__, __LINE__, __func__, #expr))
//

void f(int i) {#include <assert.h>

void fullAssert(int e) {
  assert(0 < e); // invoke standard library assert()
  (assert)(0 < ie);   // assert() macro suppressed, calling function assert()
}

...

The programmer should place nonstandard verification in a function that does not conflict with the standard library macro assert, e.g. myassert().

Code Block
bgColor#ccccff
#include <myassert.h>
#include <assert.h>

void ffullAssert(int e) {
  assert(0 < e);  customAssert// standard library assert()
  myAssert(e); // functionwell willdefined checkcustom desiredassertion assertionsfunction
}

Noncompliant Code Example (Redefining errno)

...