using assert

T

Tony Johansson

Hello Experts!

I'm reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I don' work.

The text in the book says
"To offer the programmer the choice of toggling between using and not using
asser(), this function
is used together with a macro called NDEBUG; when this macro is defined,
assert() does nothing. If NDEBUG is not defined and the actual parameter of
assert() evalutates to 0, then the name of the source file and the number of
the line on which assert() appears is displayed, and the execution of the
program is aborted by calling abort()"

If I want to use this macro I have to define it in this way
#define NDEBUG
asser(expression);
#endif

So if you want to use this NDEBUG macro I have to manually define or
undefine it.

So what the text say must be completely wrong.

//Tony
 
P

Pete Becker

Tony said:
If I want to use this macro I have to define it in this way
#define NDEBUG
asser(expression);

So if you want to use this NDEBUG macro I have to manually define or
undefine it.

So what the text say must be completely wrong.

The text is muddled. The rule is that if the macro NDEBUG is defined at
the point of the #include <assert.h> directive, the assert macro has no
effect. If the macro NDEBUG is not defined at that point, the assert
macro displays a message (which includes the file name, line number, and
the text of the expression) and calls abort(). Like this:

#include <assert.h>

void f()
{
assert(0); // aborts if called
}

#define NDEBUG
#include <assert.h>

void g()
{
assert(0); // no effect
}

#undef NDEBUG
#include <assert.h>

void h()
{
assert(0); // aborts if called
}
 
A

Andrew Koenig

The text is muddled. The rule is that if the macro NDEBUG is defined at
the point of the #include <assert.h> directive, the assert macro has no
effect. If the macro NDEBUG is not defined at that point, the assert
macro displays a message (which includes the file name, line number, and
the text of the expression) and calls abort().

Also, most C++ compilers give you a way to specify an initial value for
NDEBUG as part of compiling your program, rather than having to modify your
source code. So if you are using such a compiler (and most of us are), you
can leave NDEBUG off during testing and turn it on for production to make
your code run faster.
 
P

Pete Becker

Andrew said:
Also, most C++ compilers give you a way to specify an initial value for
NDEBUG as part of compiling your program, rather than having to modify your
source code. So if you are using such a compiler (and most of us are), you
can leave NDEBUG off during testing and turn it on for production to make
your code run faster.

Good point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top