James Raitsev
James Raitsev

Reputation: 96371

Turning off debugging, NDEBUG

C++ Primer says that

The behavior of assert depends on the status of a preprocessor variable named NDEBUG. We can "turn off" debugging by providing a #define to define NDEBUG

It is my expectation that when define is provided, asserts won't be executed.

#define NDEBUG TRUE

int main (int argc, char const *argv[])
{
    assert(argc==0);  // checked

    return 0;
}

Why, in this example, is assert statement checked, when NDEBUG is defined? (Correct me if i am wrong, but it does not matter to what it is defined, right?)

When executed from command line, using the -DNDEBUG flag, all works as expected (assert is not executed)

Upvotes: 3

Views: 3663

Answers (1)

eq-
eq-

Reputation: 10096

NDEBUG only affects assert if you define it before including <cassert> (or <assert.h>; note that you can include these headers multiple times changing the behaviour of assert depending on NDEBUG).

You don't need to define it to any specific value, or any value at all:

// this is OK
#define NDEBUG

Upvotes: 10

Related Questions