Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   using assert (http://www.velocityreviews.com/forums/t447798-using-assert.html)

Tony Johansson 08-17-2005 06:54 PM

using assert
 
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



Pete Becker 08-17-2005 07:05 PM

Re: using assert
 
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)

Andrew Koenig 08-17-2005 08:02 PM

Re: using assert
 
"Pete Becker" <petebecker@acm.org> wrote in message
news:1P2dne9ImtLkF57eRVn-jw@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.



Pete Becker 08-18-2005 06:46 PM

Re: using assert
 
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)


All times are GMT. The time now is 07:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.