Home > Back-end >  Do {... } while the meaning and usage of (0)
Do {... } while the meaning and usage of (0)

Time:03-07

the Linux kernel and other open source code, often such code:

Do {
.
} while (0)
Such code a look, it is not a cycle, the do.. While on the surface, it made no sense here, so why use?

In fact, the do {... } while (0) is greater than the effect of beautification your code, look up some information, sum up such writing mainly has the following advantages:


1, auxiliary define complex macro, avoid reference error:

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

# define DOSOMETHING () \
Foo1 (); \
Foo2 ();
This macro is meant, when calling DOSOMETHING (), the function of foo1 () and foo2 () will be invoked, but if you are in the call so write:

If (a> 0)
DOSOMETHING ();
Because it is directly by macro in pretreatment, you actually write code like this:

If (a> 0)
Foo1 ();
Foo2 ();
This is a problem, because whether a greater than zero, foo2 () will be performed, cause the program error,

So just use {} will foo1 () and foo2 () wrapped up line?

We all used in writing the code are right statements with a semicolon, if use {} in the macro, the code is equivalent to that wrote: "{... };" , unfolds just like this:

If (a> 0)
{
Foo1 ();
Foo2 ();
};
This won't even compile, so, a lot of talent has adopted the do {... } while (0);

# define DOSOMETHING () \
Do {\
Foo1 (); \
Foo2 (); \
\} while (0)

.

If (a> 0)
DOSOMETHING ();

.
So, after the macro is expanded, will retain the original semantics, GCC provides an alternative Statement - Expressions to do {... } while (0); So you can also define the macros:

# define DOSOMETHING () ({\
Foo1 (); \
Foo2 (); \
})
http://www.spongeliu.com/
2, avoid using goto to unified program flow control:

Some function, the function to return before we often do some finishing work, such as free off a function start malloc memory, goto has always been a relatively easy way:

Int foo ()
{
Somestruct * PTR=malloc (... );

Dosomething... ;
If (error)
{
Goto the END;
}

Dosomething... ;
If (error)
{
Goto the END;
}
Dosomething... ;

END:
Free (PTR);
return 0;

}
Due to the structured goto do not conform to the software engineering, and is likely to make the code to understand, so a lot of people don't advocate to use, can use at this time that the do {} while (0) to carry out unified management:

Int foo ()
{

Somestruct * PTR=malloc (... );

Do {
Dosomething... ;
If (error)
{
break;
}

Dosomething... ;
If (error)
{
break;
}
Dosomething... ;
} while (0);

Free (PTR);
return 0;

}
Here will function subject to use the do () while (0) contains, use the break to replace goto, subsequent processing work after a while, can achieve the same effect,



3, avoid empty macro caused by warning

Kernel because of the limitation of different structures are used a lot of times an empty macros, at compile time, empty macro will give a warning, in order to avoid this warning, you can use the do {} while empty (0) to define macros:

# define EMPTYMICRO do {} while (0)


4, define a separate function block to realize the complex operation:

When your function is very complex, variable many you don't want to add a function of time, using the do {} while (0); , will you write the code inside, inside can define a variable without considering the variable name in conjunction with function before or after repeated,

CodePudding user response:

Ha ha ha ha ha can
  • Related