Proper way to turn off debug macros at compile time.

Discussion in 'C Programming' started by chris.fairles@gmail.com, Aug 29, 2006.

  1. Guest

    So I took a look at assert.h because I knew it uses -DNDEBUG to turn
    off its assert statements but it does some funky stuff that I don't
    quite understand. I made my own def's something like:

    #ifdef DEBUG_LEVEL_1
    #define iprint(expr) printf(#expr " = %d\n", expr)
    #else
    #define iprint(expr)
    #endif

    #ifdef DEBUG_LEVEL_2
    #define
    #define pprint(expr) printf(#expr " = %p\n", (void *)(expr))
    #else
    #define pprint(expr)
    #endif

    But it was suggested to me that I use some do { } while(0) method or
    ((void) 0) instead of just leaving the define's blank. Whats the
    general opinion on this? (I looked in c-faq without much luck, maybe a
    good q to put in less this is implementation specific).
    , Aug 29, 2006
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote On 08/29/06 13:48,:
    > So I took a look at assert.h because I knew it uses -DNDEBUG to turn
    > off its assert statements but it does some funky stuff that I don't
    > quite understand. I made my own def's something like:
    >
    > #ifdef DEBUG_LEVEL_1
    > #define iprint(expr) printf(#expr " = %d\n", expr)
    > #else
    > #define iprint(expr)
    > #endif
    >
    > #ifdef DEBUG_LEVEL_2
    > #define
    > #define pprint(expr) printf(#expr " = %p\n", (void *)(expr))
    > #else
    > #define pprint(expr)
    > #endif
    >
    > But it was suggested to me that I use some do { } while(0) method or
    > ((void) 0) instead of just leaving the define's blank. Whats the
    > general opinion on this? (I looked in c-faq without much luck, maybe a
    > good q to put in less this is implementation specific).


    (The line immediately after the second #ifdef is
    kinda funky ...)

    The only reason I can think of to insert "code that
    does nothing" instead of "nothing" is that the former might
    silence a compiler warning in some situations. For example,
    with DEBUG_LEVEL_1 undefined in a construct like

    if (whatever > 0)
    do_something();
    else
    iprint(whatever);

    .... at least one widely-used compiler will warn you that
    the `else' is empty. (Compilers can warn about whatever
    they want, even perfectly legal C.) If the iprint() macro
    expanded to `(void)0' or some other kind of no-op, such a
    warning might not appear.

    On the other hand, some compilers that would otherwise
    have been silent might then start whining about "statement
    with no effect" or something of the kind. You cannot win
    every time!

    My own style -- not "better" or "worse" than yours --
    is to communicate my intent to the person who will someday
    get a flurry of compiler warnings and come browsing through
    the code to see what all the fuss is about:

    #ifdef DEBUG_LEVEL_1
    #define iprint(x) printf(...etc...)
    #else
    #define iprint(x) /* nil */
    #endif

    --
    Eric Sosman, Aug 29, 2006
    #2
    1. Advertising

  3. wrote:
    > So I took a look at assert.h because I knew it uses -DNDEBUG to turn
    > off its assert statements but it does some funky stuff that I don't
    > quite understand. I made my own def's something like:
    >
    > #ifdef DEBUG_LEVEL_1
    > #define iprint(expr) printf(#expr " = %d\n", expr)
    > #else
    > #define iprint(expr)
    > #endif
    >
    > #ifdef DEBUG_LEVEL_2
    > #define
    > #define pprint(expr) printf(#expr " = %p\n", (void *)(expr))
    > #else
    > #define pprint(expr)
    > #endif
    >
    > But it was suggested to me that I use some do { } while(0) method or
    > ((void) 0) instead of just leaving the define's blank. Whats the
    > general opinion on this? (I looked in c-faq without much luck, maybe a
    > good q to put in less this is implementation specific).


    One thing you should consider in addition to what is already said, is
    that ((void) 0) allows you to write

    pprint(p1), pprint(p2), pprint(p3);

    It is up to you to decide whether you want to allow that.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Aug 29, 2006
    #3
    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. mortb
    Replies:
    2
    Views:
    427
    Peter Blum
    May 10, 2004
  2. Bazza Formez
    Replies:
    4
    Views:
    573
    Bazza Formez
    Jan 27, 2005
  3. robert112
    Replies:
    1
    Views:
    438
    Juan T. Llibre
    Apr 26, 2007
  4. mk
    Replies:
    0
    Views:
    201
  5. Paul Rubin
    Replies:
    6
    Views:
    913
    Peter Otten
    Feb 12, 2010
Loading...

Share This Page