-2

Trying to find out what NDEBUG and _DEBUG actually mean in Visual Studio, I came across a cppreference page which explains that NDEBUG is about whether the assert function works in the code or not.

What does debugging have to do with the assert function? Why do we have the name NDEBUG when defining the way assert should behave?

Update To put the question more precisely, I'd like to know some implementation details. What is assert used for in debugging? What exactly do we have to assert? And why would we want to avoid such assertions?

Kaiyakha
  • 1,463
  • 1
  • 6
  • 19
  • 1
    Hint: `assert()` is not a function but a macro. Now try to find and read the definition of that macro. Also, do some research: https://stackoverflow.com/search?q=%5Bc%2B%2B%5D+what+is+assert! – Ulrich Eckhardt Dec 25 '21 at 13:58
  • Related: [_DEBUG vs NDEBUG](https://stackoverflow.com/questions/2290509/debug-vs-ndebug) – Weather Vane Dec 25 '21 at 13:59
  • Related: [What is the NDEBUG preprocessor macro used for (on different platforms)?](https://stackoverflow.com/questions/5473556/what-is-the-ndebug-preprocessor-macro-used-for-on-different-platforms) – Weather Vane Dec 25 '21 at 14:01
  • @WeatherVane I've seen that as well, actually that was the post which only puzzled me even more – Kaiyakha Dec 25 '21 at 14:02
  • And others [https://www.google.co.uk/search?q=_DEBUG+NDEBUG+assert+site%3Astackoverflow.com](https://www.google.co.uk/search?q=_DEBUG+NDEBUG+assert+site%3Astackoverflow.com) – Weather Vane Dec 25 '21 at 14:03
  • @UlrichEckhardt I know what `assert` does, the question is what it has to do with debugging – Kaiyakha Dec 25 '21 at 14:04
  • Many debuggers like the one in Visual Studio will break into the debugger on the assert(false) when you run in debug mode. That way you can look at the code and the variables that caused the assertion. – drescherjm Dec 25 '21 at 14:09
  • Does this answer your question? [What is the "assert" function?](https://stackoverflow.com/questions/1571340/what-is-the-assert-function) – Paul Hankin Dec 25 '21 at 14:19
  • 1
    A failing assert indicates a bug. It is a tool for detecting bugs. That's what it has to do with debugging. – Drew Dormann Dec 25 '21 at 14:22
  • Adding asserts can help you find places where your code breaks some invariants and may help you find your bugs (or at least hidden assumptions). I personally prefer writing small functions and unit tests (with lots of different inputs), build up bigger from there and test again. – Pepijn Kramer Dec 25 '21 at 16:00

2 Answers2

1

In the debug mode the macro NDEBUG is not defined. As a result assertions are checked.

From the C Standard (7.2 Diagnostics <assert.h>)

1 The header <assert.h> defines the assert and static_assert macros 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 <assert.h> 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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

I believe you have are mixing 2 things which is causing the confusion.

When you hear about debugging you're probably thinking about stepping through the program with breakpoints etc. Assert and NDEBUG do not (directly) have anything to do with that.

A separate question is whether the program was compiled with debug configuration. This means no or limited optimization by the compiler, added debug symbols, activated asserts and maybe more things. In order to disable asserts in the release configuration, NDEBUG is defined which deactivates crashes for failing asserts.

In order to do debugging properly, you should use a version of the application that was compiled with debug configuration. This will usually also cause failing asserts to trigger your debugger. But there are other uses for such a version. You could, for example, give it to testers so asserts are triggered if unexpected things happen.

Wutz
  • 2,246
  • 13
  • 15