Proper Way to define an ASSERT macro

Discussion in 'C++' started by Pete Becker, Feb 16, 2005.

  1. Pete Becker

    Pete Becker Guest

    Efrat Regev wrote:

    > I'd like to define an ASSERT macro that runs in debug/release modes
    > orthogonal to those of the compiler (I'm debuggin thread-races), and prints
    > file + line info. To my embarassment, I can't remember the proper way to
    > define the macro - I seem to vaguely recall something about defining
    > something like do{ ... }while(0) for some reason, but I can't remember why.
    > So, if someone could post a 3-5 line ASSERT macro, I'd really appreciate it.
    >


    Look in the header <assert.h>.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Feb 16, 2005
    #1
    1. Advertising

  2. Pete Becker

    gb Guest

    Efrat Regev wrote:

    > I'd like to define an ASSERT macro that runs in debug/release

    modes
    > orthogonal to those of the compiler (I'm debuggin thread-races), and

    prints
    > file + line info. To my embarassment, I can't remember the proper way

    to
    > define the macro - I seem to vaguely recall something about defining
    > something like do{ ... }while(0) for some reason, but I can't

    remember why.
    > So, if someone could post a 3-5 line ASSERT macro, I'd really

    appreciate it.

    You can look at the cassert or assert.h header files provided by your
    compiler to see how the its version is definied.

    When you see code like:

    do {
    int a;
    // Other stuff
    }
    while (0)

    it is usually just a way of creating a scope for the local variable.
    You could also leave out the do and the while

    {
    int a;
    // Other stuff
    }

    but some prefer the dummy do/while so it doesn't look so strange.

    In the case of the assertion, it is probably not so much for lexical
    scoping as it is for the runtime effect of creating a stack frame for a
    breakpoint.

    Gregg
    gb, Feb 16, 2005
    #2
    1. Advertising

  3. Pete Becker

    Efrat Regev Guest

    Hello,

    I'd like to define an ASSERT macro that runs in debug/release modes
    orthogonal to those of the compiler (I'm debuggin thread-races), and prints
    file + line info. To my embarassment, I can't remember the proper way to
    define the macro - I seem to vaguely recall something about defining
    something like do{ ... }while(0) for some reason, but I can't remember why.
    So, if someone could post a 3-5 line ASSERT macro, I'd really appreciate it.

    Many Thanks,

    Efrat
    Efrat Regev, Feb 16, 2005
    #3
  4. Pete Becker

    Rolf Magnus Guest

    gb wrote:

    > Efrat Regev wrote:
    >
    >> I'd like to define an ASSERT macro that runs in debug/release

    > modes
    >> orthogonal to those of the compiler (I'm debuggin thread-races), and

    > prints
    >> file + line info. To my embarassment, I can't remember the proper way

    > to
    >> define the macro - I seem to vaguely recall something about defining
    >> something like do{ ... }while(0) for some reason, but I can't

    > remember why.
    >> So, if someone could post a 3-5 line ASSERT macro, I'd really

    > appreciate it.
    >
    > You can look at the cassert or assert.h header files provided by your
    > compiler to see how the its version is definied.
    >
    > When you see code like:
    >
    > do {
    > int a;
    > // Other stuff
    > }
    > while (0)
    >
    > it is usually just a way of creating a scope for the local variable.


    That's not the only reason.

    > You could also leave out the do and the while
    >
    > {
    > int a;
    > // Other stuff
    > }
    >
    > but some prefer the dummy do/while so it doesn't look so strange.


    No, they prefer it so that things like the folling don't produce a compile
    error:

    if (foo)
    MYMACRO(whatever);
    else
    blah();
    Rolf Magnus, Feb 16, 2005
    #4
  5. Pete Becker

    gb Guest

    Rolf Magnus wrote:
    > gb wrote:
    >
    > >
    > > When you see code like:
    > >
    > > do {
    > > int a;
    > > // Other stuff
    > > }
    > > while (0)
    > >
    > > it is usually just a way of creating a scope for the local

    variable.
    >
    > That's not the only reason.


    What's another reason?

    > > You could also leave out the do and the while
    > >
    > > {
    > > int a;
    > > // Other stuff
    > > }
    > >
    > > but some prefer the dummy do/while so it doesn't look so strange.

    >
    > No, they prefer it so that things like the folling don't produce a

    compile
    > error:
    >
    > if (foo)
    > MYMACRO(whatever);
    > else
    > blah();


    You are correct, I did not consider that this construct would typically
    be found in a macro, admittedly the subject of the OP. I was thinking
    of its use in a non-macro context.

    Gregg
    gb, Feb 16, 2005
    #5
  6. Pete Becker

    gb Guest

    gb wrote:
    > Rolf Magnus wrote:
    > > gb wrote:
    > >
    > > >
    > > > When you see code like:
    > > >
    > > > do {
    > > > int a;
    > > > // Other stuff
    > > > }
    > > > while (0)
    > > >
    > > > it is usually just a way of creating a scope for the local

    > variable.
    > >
    > > That's not the only reason.

    >
    > What's another reason?


    Obviously, to create a compound statement in a macro.

    Gregg
    gb, Feb 16, 2005
    #6
    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. Dead RAM
    Replies:
    20
    Views:
    1,107
    John Harrison
    Jul 14, 2004
  2. Robert Brewer
    Replies:
    1
    Views:
    484
    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,516
    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:
    909
    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,653
    Richard Bos
    May 17, 2010
Loading...

Share This Page