Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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. It then calls the abort function.

Since Because assert() calls abort(), any cleanup functions registered with atexit() will are not be called. If the intention of the programmer is properly cleanup in the case of a failed assertion, a signal handler that calls exit() should be installed to handle SIGABRT.

Wiki Markup
See \[[ERR04-A. Choose an appropriate termination strategy]\] for more information on {{abort()}} and terminating out of programs,program termination strategies  and \[[MSC11-A. Incorporate diagnostic tests using assertions]\] for more information on using the {{assert()}} macro.

Non-Compliant Code Example

Code Block
bgColor#ffcccc
void cleanup(void) {
  /* delete temporary files, restore consistent state, etc. */
}

int main(void) {
  atexit(cleanup);

  /* ... */

  assert(/* something bad didn't happen */);

  /* ... */
}

If the assert() fails, the cleanup() will function is not be called.

Compliant Solution

...

Wiki Markup
Please note that this example is not a violation of \[[SIG30-C. Call only asynchronous-safe functions within signal handlers]\] sincebecause {{abort()}} is called synchronously by {{assert()}}.

...