Empty statement

Discussion in 'C Programming' started by Maksim Sipos, Feb 20, 2004.

  1. Maksim Sipos

    Maksim Sipos Guest

    Hello,
    I would like to have something like

    #ifdef VERSION_DEBUG
    #define DEBUG(x) DebugFnc(x)
    #end

    #ifdef VERSION_RELEASE
    #define DEBUG(x) <empty-statement>
    #end

    but I'm not sure how to implement the empty statement.. the problem, of
    course is in the extra semicolon when the macro is called as in,
    DEBUG(x); in release version. One of the possible solutions is to set

    #define DEBUG(x) do{}while(0)

    but I am not sure whether the compiler is smart enough
    to optimize this. Any other ideas, comments?

    --
    Maksim Sipos
     
    Maksim Sipos, Feb 20, 2004
    #1
    1. Advertising

  2. Maksim Sipos

    gabriel Guest

    What about:

    #define DEBUG(x) ;

    Does this work?

    --
    gabriel
     
    gabriel, Feb 20, 2004
    #2
    1. Advertising

  3. Maksim Sipos wrote:
    >
    > Hello,
    > I would like to have something like
    >
    > #ifdef VERSION_DEBUG
    > #define DEBUG(x) DebugFnc(x)
    > #end
    >
    > #ifdef VERSION_RELEASE
    > #define DEBUG(x) <empty-statement>
    > #end
    >
    > but I'm not sure how to implement the empty statement.. the problem, of
    > course is in the extra semicolon when the macro is called as in,
    > DEBUG(x); in release version.

    [...]

    Simply define it as nothing:

    #define DEBUG(x)

    Why is "the extra semicolon" a problem in the release version?

    Can you give a sample of code where it doesn't work?

    --

    +---------+----------------------------------+-----------------------------+
    | Kenneth | kenbrody at spamcop.net | "The opinions expressed |
    | J. | http://www.hvcomputer.com | herein are not necessarily |
    | Brody | http://www.fptech.com | those of fP Technologies." |
    +---------+----------------------------------+-----------------------------+
     
    Kenneth Brody, Feb 20, 2004
    #3
  4. Maksim Sipos

    David Rubin Guest

    Maksim Sipos wrote:

    > Hello,
    > I would like to have something like
    >
    > #ifdef VERSION_DEBUG
    > #define DEBUG(x) DebugFnc(x)
    > #end
    >
    > #ifdef VERSION_RELEASE
    > #define DEBUG(x) <empty-statement>
    > #end


    All that is required is

    #define DEBUG(x) /* nothing here */

    > but I'm not sure how to implement the empty statement.. the problem, of
    > course is in the extra semicolon when the macro is called as in,
    > DEBUG(x); in release version.


    The semicolon will not cause any problems since it is itself a valid "empty"
    statement. For example

    int foo(void)
    {
    int i; ; ; ;;;;
    ;
    ;;;;
    for(i=0; i<10; ++i);
    if(i > 10){
    ;
    }
    return i;
    }

    is perfectly legal. Now, imagine that each semicolon above which does not
    terminate a statement is proceeded by DEBUG(whatever).

    > One of the possible solutions is to set
    >
    > #define DEBUG(x) do{}while(0)
    >
    > but I am not sure whether the compiler is smart enough
    > to optimize this. Any other ideas, comments?


    This statement should not be optimized away since the intent is to execute the
    do-while *at least* once. This is actually a very good way to implement
    debugging macros since it protects against the dangling else problem as well as
    introduces a scope for debugging-specific variables. For example,

    if(pred)
    DEBUG(stuff);
    else
    return 0;

    If you've defined

    #define DEBUG(x) if(dbgEnabled) fprintf(stderr, "DBG: x=%d\n", x)

    you'll run into problems.

    /david

    --
    Andre, a simple peasant, had only one thing on his mind as he crept
    along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
    -- unknown
     
    David Rubin, Feb 20, 2004
    #4
  5. Maksim Sipos

    Maksim Sipos Guest

    You, as well as Kenneth Brody and Gabriel are absolutely right.
    For some reason I thought it was impossible to have hanging
    semicolons. The next question is, assume I have the following code:

    if (i == 0) DEBUG(1) ;

    In the release version (DEBUG is an empty statement), will the
    compiler create the "if i equals 0 then do nothing" or will it optimize
    it out?
    Thank you,
    Maksim Sipos

    "David Rubin" <> wrote in message
    news:c15bbl$...
    > Maksim Sipos wrote:
    >
    > > Hello,
    > > I would like to have something like
    > >
    > > #ifdef VERSION_DEBUG
    > > #define DEBUG(x) DebugFnc(x)
    > > #end
    > >
    > > #ifdef VERSION_RELEASE
    > > #define DEBUG(x) <empty-statement>
    > > #end

    >
    > All that is required is
    >
    > #define DEBUG(x) /* nothing here */
    >
    > > but I'm not sure how to implement the empty statement.. the problem, of
    > > course is in the extra semicolon when the macro is called as in,
    > > DEBUG(x); in release version.

    >
    > The semicolon will not cause any problems since it is itself a valid

    "empty"
    > statement. For example
    >
    > int foo(void)
    > {
    > int i; ; ; ;;;;
    > ;
    > ;;;;
    > for(i=0; i<10; ++i);
    > if(i > 10){
    > ;
    > }
    > return i;
    > }
    >
    > is perfectly legal. Now, imagine that each semicolon above which does not
    > terminate a statement is proceeded by DEBUG(whatever).
    >
    > > One of the possible solutions is to set
    > >
    > > #define DEBUG(x) do{}while(0)
    > >
    > > but I am not sure whether the compiler is smart enough
    > > to optimize this. Any other ideas, comments?

    >
    > This statement should not be optimized away since the intent is to execute

    the
    > do-while *at least* once. This is actually a very good way to implement
    > debugging macros since it protects against the dangling else problem as

    well as
    > introduces a scope for debugging-specific variables. For example,
    >
    > if(pred)
    > DEBUG(stuff);
    > else
    > return 0;
    >
    > If you've defined
    >
    > #define DEBUG(x) if(dbgEnabled) fprintf(stderr, "DBG: x=%d\n", x)
    >
    > you'll run into problems.
    >
    > /david
    >
    > --
    > Andre, a simple peasant, had only one thing on his mind as he crept
    > along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
    > -- unknown
     
    Maksim Sipos, Feb 20, 2004
    #5
  6. Maksim Sipos

    Eric Sosman Guest

    Maksim Sipos wrote: [top-posted; he'll learn better, we hope]
    >
    > You, as well as Kenneth Brody and Gabriel are absolutely right.
    > For some reason I thought it was impossible to have hanging
    > semicolons. The next question is, assume I have the following code:
    >
    > if (i == 0) DEBUG(1) ;
    >
    > In the release version (DEBUG is an empty statement), will the
    > compiler create the "if i equals 0 then do nothing" or will it optimize
    > it out?


    "Yes."

    That is, it's up to the compiler, and a conforming
    program can't tell whether the test was performed or not.

    There are some situations where the test must *not*
    be removed by optimization. Here are a few:

    if (i == 0) ;
    else puts ("Eeek!");


    #define i putchar('\n')
    if (i == 0) ;


    volatile int i;
    ...
    if (i == 0) ;

    --
     
    Eric Sosman, Feb 20, 2004
    #6
  7. Maksim Sipos

    xarax Guest

    "Maksim Sipos" <> wrote in message
    news:2BrZb.32323$...
    > You, as well as Kenneth Brody and Gabriel are absolutely right.
    > For some reason I thought it was impossible to have hanging
    > semicolons. The next question is, assume I have the following code:
    >
    > if (i == 0) DEBUG(1) ;
    >
    > In the release version (DEBUG is an empty statement), will the
    > compiler create the "if i equals 0 then do nothing" or will it optimize
    > it out?
    > Thank you,
    > Maksim Sipos

    /snip/

    Well, optimizations are off-topic here, but you can probably
    assume that a reasonably smart compiler will generate very
    few, if any, instructions for "if (i == 0) ;".

    --
    ----------------------------
    Jeffrey D. Smith
    Farsight Systems Corporation
    24 BURLINGTON DRIVE
    LONGMONT, CO 80501-6906
    http://www.farsight-systems.com
    z/Debug debugs your Systems/C programs running on IBM z/OS!
    Are ISV upgrade fees too high? Check our custom product development!
     
    xarax, Feb 20, 2004
    #7
  8. Maksim Sipos

    Alan Balmer Guest

    On Fri, 20 Feb 2004 17:37:02 GMT, "Maksim Sipos"
    <> wrote:

    >
    >if (i == 0) DEBUG(1) ;
    >
    >In the release version (DEBUG is an empty statement), will the
    >compiler create the "if i equals 0 then do nothing" or will it optimize
    >it out?


    Ask the compiler ;-) Or ask on a newsgroup specific to the
    implementation you're using. Unlike your first question, this one is
    off-topic, since the C standard has nothing to say on the subject.

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Feb 20, 2004
    #8
  9. "gabriel" <> wrote in message
    news:d2c9a$40362229$d01d981e$...
    > What about:
    >
    > #define DEBUG(x) ;


    [ In the following context:

    #ifdef VERSION_DEBUG
    #define DEBUG(x) DebugFnc(x)
    #end

    #ifdef VERSION_RELEASE
    #define DEBUG(x) <empty-statement>
    #end
    ]

    > Does this work?


    Of course not. Try:

    if (something_or_other)
    DEBUG(x);
    else
    DEBUG(y);

    Your solution translates to two semicolons, which in this case is a syntax
    error.

    To the OP: try something like:

    #define DEBUG(x) ((void)0)

    Neat, portable, syntactically safe and (generally) does not eat CPU cycles.

    Peter
     
    Peter Pichler, Feb 20, 2004
    #9
  10. Maksim Sipos

    Guillaume Guest

    > if (i == 0) DEBUG(1) ;
    >
    > In the release version (DEBUG is an empty statement), will the
    > compiler create the "if i equals 0 then do nothing" or will it optimize
    > it out?


    The question is: will it perform whatever is in the 'if' condition or
    not?

    If the compiler is half-decent, it will of course generate no code for
    this, provided that the condition doesn't have any side effect. If it
    does, the compiler *has* to generate the condition inside the 'if', but
    it will not generate any "flow control" code (since there is no two
    different paths).

    In your example, if 'i' is a variable, the whole 'if' statement will
    lead to no code at all. Now, if 'i' is actually a macro that could have
    side effects, the compiler has to generate the code for this macro.

    For instance:

    if (printf("Hello.\n")) ;

    The 'printf' call can't be "optimized out".
     
    Guillaume, Feb 20, 2004
    #10
  11. Maksim Sipos

    Derk Gwen Guest

    "Maksim Sipos" <> wrote:
    # Hello,
    # I would like to have something like
    #
    # #ifdef VERSION_DEBUG
    # #define DEBUG(x) DebugFnc(x)
    # #end
    #
    # #ifdef VERSION_RELEASE
    # #define DEBUG(x) <empty-statement>

    If you're not satisfied with
    #define DEBUG(x)
    then you can use
    #define DEBUG(x) 0
    or
    #define DEBUG(x) ((void)0)
    if you have whiny compiler.

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    I think that's kinda of personal; I don't think I should answer that.
     
    Derk Gwen, Feb 21, 2004
    #11
    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. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,066
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    868
    spiff
    Nov 10, 2005
  3. Marcia Hon

    Check if a directory is empty and empty it

    Marcia Hon, Feb 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    614
    Dave Thompson
    Feb 14, 2004
  4. Brian Roberts

    empty lists vs empty generators

    Brian Roberts, May 3, 2005, in forum: Python
    Replies:
    12
    Views:
    683
    Jeremy Bowers
    May 4, 2005
  5. ButlerDJIAM
    Replies:
    0
    Views:
    580
    ButlerDJIAM
    Nov 9, 2006
Loading...

Share This Page