assert()

Discussion in 'C++' started by Dave, Nov 19, 2003.

  1. Dave

    Dave Guest

    In a nutshell, what is the behavior of the assert() macro ***as proscribed
    by the C89 Standard*** (which I don't have)? Of course, it doens't appear
    in the C++ Standard since it's inherited from C. Hence my inquiry about the
    C Standard even though I care about this from a C++ perspective...

    Specifically, I'm wondering what the Standard has to say about when the
    compiler ignores asserts and when the compiler is to compile them in and
    generate code for them. After all, the Standard says nothing debug vs.
    release builds...
     
    Dave, Nov 19, 2003
    #1
    1. Advertising

  2. On Wed, 19 Nov 2003 12:50:40 -0700, Dave wrote:

    > In a nutshell, what is the behavior of the assert() macro ***as
    > proscribed by the C89 Standard*** (which I don't have)? Of course, it
    > doens't appear in the C++ Standard since it's inherited from C. Hence
    > my inquiry about the C Standard even though I care about this from a C++
    > perspective...
    >
    > Specifically, I'm wondering what the Standard has to say about when the
    > compiler ignores asserts and when the compiler is to compile them in and
    > generate code for them. After all, the Standard says nothing debug vs.
    > release builds...


    assert is defined as ((void)0) if the macro NDEBUG is defined at the point
    where <assert.h> is included. If NDEBUG is not defined, assert is supposed
    to print an error message to stderr and call abort(). The format of the
    error message is implementation-defined, but it must include the source
    file name and line number (and function name in C99).
     
    Simon Saunders, Nov 19, 2003
    #2
    1. Advertising

  3. Dave

    Gavin Deane Guest

    "Dave" <> wrote in message news:<>...
    > In a nutshell, what is the behavior of the assert() macro ***as proscribed
    > by the C89 Standard*** (which I don't have)? Of course, it doens't appear
    > in the C++ Standard since it's inherited from C. Hence my inquiry about the
    > C Standard even though I care about this from a C++ perspective...
    >
    > Specifically, I'm wondering what the Standard has to say about when the
    > compiler ignores asserts and when the compiler is to compile them in and
    > generate code for them. After all, the Standard says nothing debug vs.
    > release builds...


    I don't have the C standard either, but in the C++ standard,
    17.4.2.1/2 mentions the NDEBUG macro. And my understanding is that
    when this macro is not defined, code is generated for an assert, and
    when NDEBUG is defined code is not generated.

    This ties in with the contents of my implementation's assert.h file.

    GJD
     
    Gavin Deane, Nov 20, 2003
    #3
  4. EventHelix.com, Nov 20, 2003
    #4
  5. Dave

    NFish Guest

    EventHelix.com wrote:

    > Checkout the following article for assert handling:
    >
    > http://www.eventhelix.com/RealtimeMantra/Object_Oriented/design_by_contract.htm
    >
    > Sandeep


    Their non-debugging macros do not *not* evaluate their arguments; e.g.

    ASSERT(strlen(foo) > 10);

    gets expanded to

    strlen(foo) > 10;

    when the _DEBUG flag is not defined. Their code is full of similar
    examples where assert macros generate overhead no matter what. Pretty
    poor example.
     
    NFish, Nov 20, 2003
    #5
    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. Mark Newby
    Replies:
    1
    Views:
    1,714
  2. Robert Brewer
    Replies:
    1
    Views:
    505
    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,538
    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:
    937
    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,714
    Richard Bos
    May 17, 2010
Loading...

Share This Page