Do {...} Meanings and usage of while (0)

Source: Internet
Author: User

From: http://blogread.cn/it/article/5907

Linux Kernel and some other open-source code often encounter such code:

do{ ...}while(0)

Such code is not a loop at first glance. Do... while has no meaning at all on the surface, so why should we use it like this?

In fact, do {...} while (0) is much more effective than beautifying your code. I checked some information and summarized the following advantages:

1. Help define complex macros to avoid errors when referencing:

For example, suppose you need to define a macro like this:

#define DOSOMETHING()\\               foo1();\\               foo2();

This macro is intended to call both the foo1 () and foo2 () functions when dosomething () is called. However, if you write this when calling:

if(a>0)    DOSOMETHING();

Because the macro is directly expanded during preprocessing, the code you actually write looks like this:

if(a>0)    foo1();foo2();

This causes a problem, because no matter whether a is greater than 0, foo2 () will be executed, resulting in program errors.

So can I only use {} to package foo1 () and foo2?

When writing code, we are used to adding a semicolon to the right of the statement. If {} is used in the macro, the code is equivalent to writing "{...}; ", which is like this after expansion:

if(a>0){    foo1();    foo2();};

This will not even pass the compilation. Therefore, many people use do {...} while (0 );

#define DOSOMETHING() \\        do{ \\          foo1();\\          foo2();\\        }while(0)\\    ... if(a>0)    DOSOMETHING(); ...

In this way, after the macro is expanded, the initial semantics is retained. GCC provides statement-expressions to replace do {...} while (0); so you can define the macro as follows:

#define DOSOMETHING() ({\\        foo1(); \\        foo2(); \\})

Http://www.spongeliu.com/

2. Avoid using goto to uniformly control program streams:

In some functions, we often do some final work before the return function. For example, if you drop a function and start malloc memory, Goto is always a simple method:

int foo(){    somestruct* ptr = malloc(...);     dosomething...;    if(error)    {        goto END;    }     dosomething...;    if(error)    {        goto END;    }    dosomething...; END:    free(ptr);    return 0; }

Because goto does not conform to the structure of software engineering and may make the code difficult to understand, many people do not advocate the use of it. At this time, do {} while (0) can be used) for unified management:

int foo(){     somestruct* ptr = malloc(...);     do{        dosomething...;        if(error)        {            break;        }         dosomething...;        if(error)        {            break;        }        dosomething...;    }while(0);     free(ptr);    return 0; }

Here, we use do () while (0) to include the function subject and break to replace Goto. Subsequent processing can achieve the same effect after the while operation.

3. Avoid warning caused by empty macros

Due to the limitations of different architectures, empty macros are often used in the kernel. during compilation, empty macros provide warning. To avoid such warning, you can use do {} while (0) to define an empty macro:

#define EMPTYMICRO do{}while(0)

4. Define a separate function block for complex operations:

When your functions are complex and you don't want to add a function with many variables, use do {} while (0); to write your code in it, you can define variables without having to consider repeated variable names before or after the function.

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.