Skip to contentSkip to conversation

Significance of do ... while (0)

DISCUSSION krishna 5 participants 7 messages Dec 17, 2004

lore
Share & tools
lore

krishna

author
#0 lore raw

Hi all,

Can any one explain the importance of do ... while (0)

Regards,
Krishna Chaitanya

#define STATS_SET_HIGH(x)       do { if ((x)->num_active > (x)->high_mark) \
                                       (x)->high_mark = (x)->num_active; \
                               } while (0)
#define STATS_INC_ERR(x)        ((x)->errors++)
#define STATS_SET_FREEABLE(x, i) \
                               do { if ((x)->max_freeable < i) \
                                       (x)->max_freeable = i; \
                               } while (0)
#define STATS_INC_ALLOCHIT(x)   atomic_inc(&(x)->allochit)
#define STATS_INC_ALLOCMISS(x)  atomic_inc(&(x)->allocmiss)
#define STATS_INC_FREEHIT(x)    atomic_inc(&(x)->freehit)
#define STATS_INC_FREEMISS(x)   atomic_inc(&(x)->freemiss)
#else
#define STATS_INC_ACTIVE(x)     do { } while (0)
#define STATS_DEC_ACTIVE(x)     do { } while (0)
#define STATS_INC_ALLOCED(x)    do { } while (0)
#define STATS_INC_GROWN(x)      do { } while (0)
#define STATS_INC_REAPED(x)     do { } while (0)
#define STATS_SET_HIGH(x)       do { } while (0)
#define STATS_INC_ERR(x)        do { } while (0)
#define STATS_SET_FREEABLE(x, i) \
                               do { } while (0)
#define STATS_INC_ALLOCHIT(x)   do { } while (0)
#define STATS_INC_ALLOCMISS(x)  do { } while (0)
#define STATS_INC_FREEHIT(x)    do { } while (0)
#define STATS_INC_FREEMISS(x)   do { } while (0)
#5 lore raw
Quoted 10 lines
Quoted 5 lines
Quoted 1 line

Can any one explain the importance of do ... while (0)

It's a standard C programming practice; more info is available from your local search engine:
http://www.google.com/search?lr=&c2coff=1&q=%22while%280%29%22

Why? {} would suffice, no need to do{}while(0)

Quoted 9 lines

Jan Engelhardt
--
ENOSPC
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Unless I'm mistaken, it won't work in all cases. Example:

    #define foo(...) { ... /* nefarious macro stuff here */ ... }

    if (some_condition)
        foo();
    else
        // do something nifty

The above would expand to:

    if (some_condition) {
        /* nefarious macro stuff here */
    };
    else
        // do something nifty

And that's not quite right.

-Vadim Lobanov

#6 lore raw
Quoted 13 lines
Quoted 3 lines
Quoted 2 lines
Quoted 1 line

Can any one explain the importance of do ... while (0)

It's a standard C programming practice; more info is available from

Why? {} would suffice, no need to do{}while(0)

Unless I'm mistaken, it won't work in all cases. Example:

   #define foo(...) { ... /* nefarious macro stuff here */ ... }
   if (some_condition)
       foo();
   else
       // do something nifty

The above would expand to:

Quoted 7 lines
   if (some_condition) {
       /* nefarious macro stuff here */
   };
   else
       // do something nifty

And that's not quite right.

So it's best to always make some braces around ifs :) Would be consistent with longer-than-one-statement blocks too.

if(some_cond) {
MY_MACRO
}

Jan Engelhardt

Signature · 1 line
ENOSPC