Do {...} Clever Use of while (0)

Source: Internet
Author: User

In the Linux KernelCodeHas such macro definition:
# Define dump_write (ADDR, NR) do {memcpy (bufp, ADDR, NR); bufg + = nR;} while (0)

This macro definition means that the loop body will be executed once when this macro operation is referenced, but why is it defined as such a strange form?

Let's take a look at the macro definitions in several other forms:
# Define dump_write (ADDR, NR) memcpy (bufp, ADDR, NR); bufp + = nR;

Is this macro definition acceptable?

We put this macro in a context:
If (ADDR)

Dump_write (ADDR, NR );

Else

Do_something_else ();

The result shows an error occurs during compilation. Why?

Expand the macro, and the preceding statement block is changed:
If (ADDR)

Memcpy (bufp, ADDR, NR); bufp + = nR;

Else

Do_something_else ();

In this way, a statement bufp + = nR is added between the IF-else statement to form a statement similar to if-dosomething-Else. In this way, else will not be able to find the matching if, so the compiler will report an error.

Alternative reference format:
If (! ADDR)

Do_something_else ();

Else

Dump_write (ADDR, NR );

In this way, compilation is successful, but the problem is more serious, because bufp + = nR; will always be executed.

Here, we may immediately consider adding a braces in the macro definition:

# Define dump_write (ADDR, NR) {memcpy (bufp, ADDR, NR); bufp + = nR ;}

Can this work? Let's test it in context:

If (ADDR)

Dump_write (ADDR, NR );

Else

Do_something_else ();

Still compilation error. Why?

Expand the macro:

If (ADDR)

{Memcpy (bufp, ADDR, NR); bufp + = nR ;};

Else

Do_something_else ();

Here, an empty statement is inserted in the IF-else statement: If-null statement-else. The compiler cannot find the matching if statement for else. Therefore, an error will still be reported during compilation.

Using the macro definition in the following format is completely correct.

# Define dump_write (ADDR, NR) do {memcpy (bufp, ADDR, NR); bufg + = nR;} while (0)

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.