By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,977 Members | 1,610 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,977 IT Pros & Developers. It's quick & easy.

using assert

P: n/a
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
Aug 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tony Johansson wrote:

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
}

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 17 '05 #2

P: n/a
"Pete Becker" <pe********@acm.org> wrote in message
news:1P********************@rcn.net...
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.
Aug 17 '05 #3

P: n/a
Andrew Koenig wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.