X
تبلیغات
رایتل

طراح

نکته ای در تعریف ماکروها در زبان C

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.

Since the conditional expression of the do loop is a constant value, the compiler will not generate any additional code to process the loop, so this idiom does not add any overhead to the code when it is executed.

This technique is frequently used in macro definitions where the body of the macro is not a single or simple statement. Since you do not know where the macro will be used, it is important that body of the macro has a syntax that is equivalent to the statement it replaces. For example, the following macro will replace a single statement with two statements.

#define foo \

statement_1; \

statement_2

If this macro is used in an if statement like the one below, it will not have the intended effect.

if (condition)

foo;

After replacing the macro in this code, the if condition will be followed by two statements rather than one. This is probably not what you want because the if condition will only control the execution of the first statement.

if (condition)

statement_1;

statement_2;

The following example shows what the macro looks like when it is rewritten using the do loop idiom. Notice there is no ending semicolon in the definition; the semicolon will be provided by the statement that is replaced.

#define foo \

do { \

statement_1; \

statement_2; \

} while (0)

Now, when the macro is used, the if condition is followed by a single, compound statement, which will behave as you would expect.

if (condition)

do {

statement_1;

statement_2;

} while (0);

A similar problem occurs when the body of the macro contains an if statement without an else statement.

#define foo \

if (condition) \

statement

If this macro is used in another if statement that does have an else statement, the if statement in the macro body will steal the else that follows it.

if (condition)

foo;

else

bar;

Although the indentation of the else statement shows the intention that it should belong to the first if statement, it will actually be associated with the second if statement.

if (condition)

if (condition)

statement;

else

bar;

Using the do loop idiom in this instance, will terminate the if statement in the macro, so that it will not steal an else statement that happens to follow it.

if (condition)

do {

if (condition)

statement;

} while (0);

else

bar;

Using an empty do-while(0) loop is also a common way to write an empty statement in a macro definition. In older compilers, this was necessary to prevent the compiler from issuing a warning. For example:

#define do_nothing do {} while (0)

The current gcc compiler provides another method that can be used in place of the do-loop idiom as shown in the following example.

#define foo ({ \

statement_1; \

statement_2; \

})

نظرات (0)
نام :
ایمیل : [پنهان میماند]
وب/وبلاگ :
برای نمایش آواتار خود در این وبلاگ در سایت Gravatar.com ثبت نام کنید. (راهنما)