Subject: Question on macros...
To: None <tech-kern@netbsd.org>
From: Johnny Billquist <bqt@softjar.se>
List: tech-kern
Date: 02/26/2007 16:04:36
I've just noticed a pattern in macros that I never have thought about 
before, and that I don't understand the point of.
Perhaps someone here can enlighten me.

In quite a few places in the kernel, we have macros, that are defined 
like this:

#define somemacro(x) \
   do { \
     somestuff; \
   } while (0)

Now, why the do-while loop here? I have no problem with the fact that 
somestuff should be in a block, but a while-loop that will only be run 
once??? Why not just:

#define somemacro(x) \
   { \
     somestuff; \
   }

As far as my knowledge of C goes, that should be equivalent, unless 
possibly you would like to use break or continue to get out of the block.

Can anyone enlighten me?

	Johnny