9

I'm working on C++ program built by other people, and saw a lot of uses of DEBUG like this

#ifdef DEBUG
    cout << "Value is "<< value << endl;
#endif

I myself am still in the learning process to become an affluent C++ programmer, and I majorly use Visual Studio and breakpoints to debug. So I'm wondering, if I'm able to step through the code to debug values, is there any other reason to use these kind of macros?

Tried to google but didn't find much useful page.

Thanks.

Derek
  • 1,085
  • 2
  • 20
  • 36
  • when an answer actually provides you with the (best) solution to your problem you can click on the big V next to it to accept it. – KillianDS May 31 '12 at 21:35

7 Answers7

10

Sometimes you don't want to step through the whole code, but just inspect the output in the terminal.

If the code is compiled with DEBUG defined, probably in a debug build, you see the output. For a release build, you don't. If you go to project settings -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions, you'll see that DEBUG is defined for the debug build, but it's not for release. (I actually have _DEBUG)

Imagine you have a huge function, and what you're interested in is at the 1000th line (it's old code, can't change it). Would you rather step through all of that messy legacy code or have helpful debug statements at key points? Would you rather the console tell you where something went wrong, or set breakpoints at each of the 237 return statements at fail locations?

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 3
    I'd like to add, that `_DEBUG` is specific for Visual C++. Check [Enabling Debug Features in Visual C++](http://msdn.microsoft.com/en-us/library/5bb575z2.aspx?ppud=4) – LihO May 31 '12 at 21:45
7

While you're debugging, it is a common practice to dump some intermediate values on the screen. The visual debugger is not always helping, because you waste a lot of time manipulating with mouse.

The need for "text-mode debugging" and logging also frequently comes from the embedded systems experience, where you don't have much visual aid and all you can do is to dump a byte or two to serial port or something like that. When you get used to quickly finding critical debug points, you just insert some printing code there whose value checks the program correctness.

The "DEBUG" macro is defined by MSVC++ compiler while your project is compiled in Debug mode. When you're making the Release version, all the code which is actually useless for the end-users gets "stripped away" by the preprocessor.

Typical snippet

#ifdef DEBUG
Some code
#endif

is removed by the preprocessor if the DEBUG is not defined.

Viktor Latypov
  • 14,289
  • 3
  • 40
  • 55
3

It can be handy to use console output rather than a debugger for identifying multithreading bugs. Interrupting the flow of the program via a breakpoint often stops the bug from occurring because it stops the threads stepping on each other's toes. The same is true of other timing-based bugs.

Ant
  • 4,890
  • 1
  • 31
  • 42
2

"I majorly use Visual Studio and breakpoints to debug"
Debugging your code by watching its behaviour step by step is quite hard or even impossible in some situations. Sometimes it's just easier to create this kind of "debug output" so that you see what's going on from these logs instead of trying to step through it in real time.

Checking whether DEBUG symbol is defined is to make sure that your release version doesn't make this kind of output. Note that Visual Studio defines _DEBUG for debug configuration. More specifically: "The compiler defines _DEBUG when you specify the /MTd or /MDd option. These options specify debug versions of the C run-time library." There is also NDEBUG that disables C-style assertions when defined. For more information check _DEBUG vs NDEBUG.

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
1

It's for debugging, by wrapping the code in preprocessor commands you can turn that code on or off.

Take a look here: C++ Notes: Preprocessor

Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
1

Easy, when you want to get some messages that may help you to make a "soft" debug you just define DEBUG and the sentences between #ifdef DEBUG and #endif will make effect and in your case get some useful messages.

This way, when you finished developing and you want to make a release you just undefine debug and the messages will appear no more.

You may think yes, it is a good idea, but is more code for the app but the good point is that those are macros and are evaluated at compile time, therefore, the app will be the same that if you delete all of them :)

Mario Corchero
  • 5,257
  • 5
  • 33
  • 59
0

I'd recommend not using a DEBUG macro. Instead, use the standard NDEBUG macro, which is defined when debugging code is not wanted, rather than defined when debugging code is wanted. That is, have debugging code active by default. You will find that only a small core of performance critical code needs to have debugging checks switched off to get adequate performance.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • In SO question [_DEBUG vs NDEBUG](http://stackoverflow.com/questions/2290509/debug-vs-ndebug) and also [this SO question](http://stackoverflow.com/questions/5473556/what-is-the-ndebug-preprocessor-macro-used-for-on-different-platforms) the use of `NDEBUG` for anything else than controlling the behaviour of assertions is not recommended. The reason being the way how `NDEBUG` is allowed to be defined/undefined/redefined arbitrarily. – herzbube Dec 09 '12 at 13:01