tags:

views:

685

answers:

3
+3  Q: 

_DEBUG vs NDEBUG

Do you prefer #ifdef _DEBUG or #ifndef NDEBUG to specify debug sections of code? Is there a better way to do it? e.g. #define MY_DEBUG.

I think _DEBUG is Visual Studio specific, is NDEBUG standard?

+2  A: 

Be consistent and it doesn't matter which one. Also if for some reason you must interop with another program or tool using a certain DEBUG identifier it's easy to do

#ifdef THEIRDEBUG
#define MYDEBUG
#endif //and vice-versa
Earlz
+11  A: 

Visual Studio defines _DEBUG when you specify the /MTd or /MDd option, NDEBUG enables standard-C assertions. Use them when apropriate, ie _DEBUG if you want your debugging code to be consistent with the MS CRT debugging techniques and NDEBUG if you want to be consistent with assert().

If you define your own debugging macros (and you don't hack the compiler or C runtime), avoid starting names with an underscore, as these are reserved.

Christoph
+1. NDEBUG in particular is allowed to be #undef'd and #define'd within a single TU (and reincluding <assert.h> changes the assert macro accordingly). Because this is different than expected/desired, it's common to use another macro to control a "compile-wide" debug flag as this answer says.
Roger Pate
+1  A: 

The macro NDEBUG controls whether assert() statements are active or not.

In my view, that is separate from any other debugging - so I use something other than NDEBUG to control debugging information in the program. What I use varies, depending on the framework I'm working with; different systems have different enabling macros, and I use whatever is appropriate.

If there is no framework, I'd use a name without a leading underscore; those tend to be reserved to 'the implementation' and I try to avoid problems with name collsions - doubly so when the name is a macro.

Jonathan Leffler