Do {} while (0)

Source: Internet
Author: User

It is recommended that programmers skip this article and view the original article directly.

You may see a do loop where the conditional expression is constant 0. it creates a loop that will only be executed once. this programming method can place multi-row Macros in any place where only one statement can be used.

Since the condition expression of the do loop is a constant, the compiler does not generate additional code to process the loop. Therefore, when executing this code, this method does not add additional operations.

This technology is frequently used in macro definition, especially when macro content is complex (such as multiple statements. because I do not know where macros are used, it is important to make macros meet the syntax required after they are replaced. for example, the following macro replaces two statements with one statement.

#define foo \        statement_1; \        statement_2

If this macro is used in the following if statement, it will not produce the expected results:

if (condition)    foo;

After the macro is replaced, the IF condition is followed by two statements. This is probably not what you want, because the if statement only controls the execution of the first statement.

if (condition)    statement_1;    statement_2;

The following example is rewritten using the do loop method. NOTE: If there is no ending semicolon in the definition, the semicolon will be provided by the replaced statement. (Note: This makes macro statements more like a legal statement. if a semicolon exists in the macro definition, it does not need to be added when calling the Macro. It looks abrupt .)

#define foo \        do { \            statement_1; \            statement_2; \        } while (0)

Now, when this macro is used, as you expected, the IF condition is followed by a statement. (Note: The following statement is the result after expansion)

if (condition)    do {        statement_1;        statement_2;    } while (0);

A similar problem occurs when the macro only contains the if statement and does not contain the else statement.

#define foo \        if (condition) \        statement

If the macro is used in an if statement with an else statement, if in the macro will steal the else statement following it.

if (condition)    foo;else    bar;

Although the indentation of the else statement indicates that it belongs to the first if statement, it is combined with the second else statement.

if (condition)    if (condition)        statement;else    bar;

If the do loop method is used in this example, the IF statement ends in the macro, so the following else statement is not stolen.

if (condition)    do {        if (condition)            statement;    } while (0);else    bar;

Using an empty do-while (0) loop is also a common method for defining null statement macros. In the old compiler, it is necessary to prevent the compiler from generating warnings. For example:

#define do_nothing do {} while (0)

The current GCC compiler provides another method that can replace do-while, for example:

#define foo ({ \        statement_1; \        statement_2; \})

Translation completed.

Note: The Max Implementation of Linux 2.4.31 kernel is as follows:

#define max(x,y) ({ \        const typeof(x) _x = (x);       \        const typeof(y) _y = (y);       \        (void) (&_x == &_y);            \        _x > _y ? _x : _y; })

To avoid repeated calculation of Max parameters, the macro definition uses typeof, which is a GNU extension used to obtain the expression type. typeof does not seem to calculate the value of the expression. I will post a new post to introduce the max function.

Original article:

Bruce BlinN:
Do {} while (0)

Reference:

Bit twiddling hacks

Gcc doc:
Referring to a type with typeof

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.