Search code examples
c++cassertexitabort

What are the advantages or implications of using assert vs exit or vice versa?


As far as I understand, a call to assert(e), where e is a boolean expression, executes something roughly like

if (!e) {
    printf("%s:%d: failed assertion `%s'\n", __FILE__, __LINE__, e); 
    abort();
}

which terminates the program abruptly if the given expression is not true.

On the other hand, instead of using assert, I could probably write something like

if (!e) {
    fprintf(stderr, "custom error message.\n");
    exit(1);
}

which naively feels like a more cleaner, nicer thing to do.

Ignoring that assert can be turned off globally using the NDEBUG flag, what other advantage do you think one has over the other? Have I got the distinction right, or are there conceptual differences between the two which I am unaware of that let the two ways of conditional program termination have their own niche use cases? If the latter, please explain. Thank you.


Solution

  • what other advantage do you think one has over the other?

    A macro is used because you want to be able to remove it via conditional compilation. In other words, you don't want the code to even appear in the binary.

    Have I got the distinction right, or are there conceptual differences between the two which I am unaware of that let the two ways of conditional program termination have their own niche use cases?

    Well, exit() and abort() don't behave the same way even if you use 1 as a "unsuccessful" exit code. The latter is intended to kill the program right away without further work and possibly trigger a debugging prompt or save an image of the process space (although exactly what it does depends on the vendor providing it). The former calls the registered functions by atexit(). There are other ways of stopping, too, see quick_exit() and _Exit().

    For C++, there are way more considerations on the behavioral difference, e.g. whether destructors for variables in the stack frame(s) are run, whether global destructors are run, what happens if an exception is thrown while doing that, etc.