csdn's FAQ:ide
一直觉得Linux里面,那些do{}while(0)只是为了程序的源代码看起来比较好看而已
今天据说他是有特殊做用的,在线请教,是什么做用?
---------------------------------------------------------------
是为了解决使用宏的时候烦人的分号问题。
---------------------------------------------------------------
楼说的不是很全面,我给个例子吧
#define wait_event(wq,condition) /
do{ if(condition) break; __wait_event(wq,condition); }while(0)
这是一个奇怪的循环,它根本就只会运行一次,为何不去掉外面的do{..}while结构呢?我曾一度在内心把它叫作“怪圈”。原来这也是很是巧妙的技巧。在工程中可能常常会引发麻烦,而上面的定义可以保证这些麻烦不会出现。下面是解释:
假设有这样一个宏定义
#define macro(condition) if(condition) dosomething();
如今在程序中这样使用这个宏:
if(temp)
macro(i);
else
doanotherthing();
一切看起来很正常,可是仔细想一想。这个宏会展开成:
if(temp)
if(condition) dosomething();
else
doanotherthing();
这时的else不是与第一个if语句匹配,而是错误的与第二个if语句进行了匹配,编译经过了,可是运行的结果必定是错误的。
为了不这个错误,咱们使用do{….}while(0) 把它包裹起来,成为一个独立的语法单元,从而不会与上下文发生混淆。同时由于绝大多数的编译器都可以识别do{…}while(0)这种无用的循环并进行优化,因此使用这种方法也不会致使程序的性能下降。
---------------------------------------------------------------
但是直接用{}括起来的话,最后的分号会引发麻烦的
---------------------------------------------------------------
但这样就必定要在最后加分号,不能看成表达式用了。
唉,仍是尽可能避免用宏替换的方法,太容易出现问题了。性能
**************************************************************** 优化
FAQ FROM CSDN:this
Why do a lot of #defines in the kernel use do { ... } while(0)?spa
There are a couple of reasons:code
(from Dave Miller) Empty statements give a warning from the compiler so this is why you see #define FOO do { } while(0).orm
(from Dave Miller) It gives you a basic block in which to declare local variables.get
(from Ben Collins) It allows you to use more complex macros in conditional code. Imagine a macro of several lines of code like:编译器
#define FOO(x) / printf("arg is %s/n", x); / do_something_useful(x);
if (blah == 2) FOO(blah);
if (blah == 2) printf("arg is %s/n", blah); do_something_useful(blah);;
As you can see, the if then only encompasses the printf(), and the do_something_useful() call is unconditional (not within the scope of the if), like you wanted it. So, by using a block likedo { ... } while(0), you would get this:
if (blah == 2) do { printf("arg is %s/n", blah); do_something_useful(blah); } while (0);
(from Per Persson) As both Miller and Collins point out, you want a block statement so you can have several lines of code and declare local variables. But then the natural thing would be to just use for example:
#define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
However that wouldn't work in some cases. The following code is meant to be an if-statement with two branches:
if (x > y) exch(x,y); // Branch 1 else do_something(); // Branch 2
But it would be interpreted as an if-statement with only one branch:
if (x > y) { // Single-branch if-statement!!! int tmp; // The one and only branch consists tmp = x; // of the block. x = y; y = tmp; } ; // empty statement else // ERROR!!! "parse error before else" do_something();
The problem is the semi-colon (;) coming directly after the block. The solution for this is to sandwich the block between do and while (0). Then we have a single statement with the capabilities of a block, but not considered as being a block statement by the compiler. Our if-statement now becomes:
if (x > y) do { int tmp; tmp = x; x = y; y = tmp; } while(0); else do_something();