using assert

Discussion in 'C++' started by Tony Johansson, Aug 17, 2005.

  1. 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
    Tony Johansson, Aug 17, 2005
    #1
    1. Advertising

  2. Tony Johansson

    Pete Becker Guest

    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)
    Pete Becker, Aug 17, 2005
    #2
    1. Advertising

  3. "Pete Becker" <> wrote in message
    news:...

    > 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.
    Andrew Koenig, Aug 17, 2005
    #3
  4. Tony Johansson

    Pete Becker Guest

    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)
    Pete Becker, Aug 18, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edvard Majakari

    Using debug print routine inside assert

    Edvard Majakari, Nov 4, 2003, in forum: Python
    Replies:
    13
    Views:
    600
    Alex Martelli
    Nov 5, 2003
  2. Robert Brewer
    Replies:
    1
    Views:
    487
    bsmith
    Nov 7, 2004
  3. Thomas Guettler

    assert 0, "foo" vs. assert(0, "foo")

    Thomas Guettler, Feb 23, 2005, in forum: Python
    Replies:
    3
    Views:
    2,520
    Carl Banks
    Feb 23, 2005
  4. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    914
    Keith Thompson
    Nov 25, 2004
  5. ImpalerCore

    To assert or not to assert...

    ImpalerCore, Apr 27, 2010, in forum: C Programming
    Replies:
    79
    Views:
    1,661
    Richard Bos
    May 17, 2010
Loading...

Share This Page