3

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)

1 Answer 1

10

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.