Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I completely disable calls to assert()?

Tags:

c++

c

assert

My code is full of calls to assert(condition). In the debug version I use g++ -g which triggers my assertions. Unexpectedly, the same assertions are also triggered in my release version, the one compiled without -g option.

How can I completely disable my assertions at compile time? Should I explicitly define NDEBUG in any build I produce regardless of whether they are debug, release or anything else?

like image 718
Abruzzo Forte e Gentile Avatar asked Mar 18 '11 15:03

Abruzzo Forte e Gentile


People also ask

How do I turn off assert statements?

We can disable assertions in a program by using NDEBUG macro. Using NDEBUG macro in a program disables all calls to assert.

How do I disable assert in Python?

Using the -O flag (capital O) disables all assert statements in a process.

How do I disable assert in Visual Studio?

In Visual Studio, go to Debug / Windows / Exception Settings. In the Exception Settings, go to Win32 Exceptions / 0xc0000420 Assertion Failed. Uncheck the box in front of that entry.

What is Ndebug?

The macro NDEBUG denotes not using debugging information. If NDEBUG is defined, then assert is defined as an expression which does nothing. Otherwise assert will print debugging information if the expression it tests is false.


2 Answers

You must #define NDEBUG (or use the flag -DNDEBUG with g++) this will disable assert as long as it's defined before the inclusion of the assert header file.

like image 119
GWW Avatar answered Oct 13 '22 05:10

GWW


Use #define NDEBUG

7.2 Diagnostics

1 The header defines the assert macro and refers to another macro,

NDEBUG

which is not defined by <assert.h>. If NDEBUG is defined as a macro name at the point in the source file where is included, the assert macro is defined simply as

#define assert(ignore) ((void)0)

The assert macro is redefined according to the current state of NDEBUG each time that <assert.h> is included.

like image 41
Prasoon Saurav Avatar answered Oct 13 '22 06:10

Prasoon Saurav