Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use apparently meaningless do-while and if-else statements in macros?

In many C/C++ macros I'm seeing the code of the macro wrapped in what seems like a meaningless do while loop. Here are examples.

#define FOO(X) do { f(X); g(X); } while (0) #define FOO(X) if (1) { f(X); g(X); } else 

I can't see what the do while is doing. Why not just write this without it?

#define FOO(X) f(X); g(X) 
like image 872
jfm3 Avatar asked Sep 30 '08 17:09

jfm3


People also ask

Why it is better to use a macro in place of a function?

The speed at which macros and functions differs. Macros are typically faster than functions as they don't involve actual function call overhead.

What are the advantages of using macro in place of function and when will be a macro used?

When writing macros for functions, they saves a lot of time that is spent by the compiler for invoking / calling the functions. Hence, The advantage of a macro over an actual function, is speed. No time is taken up in passing control to a new function, because control never leaves the home function.

What is the purpose of do while 0?

You may see a do loop with the conditional expression set to a constant value of zero (0). This creates a loop that will execute exactly one time. This is a coding idiom that allows a multi-line macro to be used anywhere that a single statement can be used.

Is it better to use macro or function?

Macros have the distinct advantage of being more efficient (and faster) than functions, because their corresponding code is inserted directly into your source code at the point where the macro is called. There is no overhead involved in using a macro like there is in placing a call to a function.


1 Answers

The do ... while and if ... else are there to make it so that a semicolon after your macro always means the same thing. Let's say you had something like your second macro.

#define BAR(X) f(x); g(x) 

Now if you were to use BAR(X); in an if ... else statement, where the bodies of the if statement were not wrapped in curly brackets, you'd get a bad surprise.

if (corge)   BAR(corge); else   gralt(); 

The above code would expand into

if (corge)   f(corge); g(corge); else   gralt(); 

which is syntactically incorrect, as the else is no longer associated with the if. It doesn't help to wrap things in curly braces within the macro, because a semicolon after the braces is syntactically incorrect.

if (corge)   {f(corge); g(corge);}; else   gralt(); 

There are two ways of fixing the problem. The first is to use a comma to sequence statements within the macro without robbing it of its ability to act like an expression.

#define BAR(X) f(X), g(X) 

The above version of bar BAR expands the above code into what follows, which is syntactically correct.

if (corge)   f(corge), g(corge); else   gralt(); 

This doesn't work if instead of f(X) you have a more complicated body of code that needs to go in its own block, say for example to declare local variables. In the most general case the solution is to use something like do ... while to cause the macro to be a single statement that takes a semicolon without confusion.

#define BAR(X) do { \   int i = f(X); \   if (i > 4) g(i); \ } while (0) 

You don't have to use do ... while, you could cook up something with if ... else as well, although when if ... else expands inside of an if ... else it leads to a "dangling else", which could make an existing dangling else problem even harder to find, as in the following code.

if (corge)   if (1) { f(corge); g(corge); } else; else   gralt(); 

The point is to use up the semicolon in contexts where a dangling semicolon is erroneous. Of course, it could (and probably should) be argued at this point that it would be better to declare BAR as an actual function, not a macro.

In summary, the do ... while is there to work around the shortcomings of the C preprocessor. When those C style guides tell you to lay off the C preprocessor, this is the kind of thing they're worried about.

like image 199
jfm3 Avatar answered Oct 08 '22 17:10

jfm3