zz:do{}while(0)の意味

4270 ワード

csdn's FAQ:
ずっとLinuxの中で、あれらのdo{}while(0)はただプログラムのソースコードのために比较的にきれいに见えるだけだと思っています今日彼が特殊な作用があると闻いて、オンラインで教えてもらって、どんな作用ですか?------------------------------------------------------------------マクロを使うときの煩わしいセミコロンの問題を解決するためです.------------------------------------------------------------------------------------------------------------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は1番目のif文と一致するのではなく、2番目のif文と誤って一致し、コンパイルは通過したが、実行結果は間違いに違いない.このエラーを回避するためにdo{....}を使用します.while(0)はそれを包み、コンテキストと混同されない独立した文法ユニットとなる.また,ほとんどのコンパイラはdo{...}while(0)という不要なループを識別して最適化できるため,この方法を用いてもプログラムの性能が低下することはない.----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------まあ、マクロに置き換える方法は避けたほうが問題になりやすいです.
**************************************************************** 
FAQ FROM CSDN:
FAQ/DoWhile0
Why do a lot of#defines in the kernel use do { ... } while(0)?
There are a couple of reasons:
  • (from Dave Miller) Empty statements give a warning from the compiler so this is why you see #define FOO do { } while(0).
  • (from Dave Miller) It gives you a basic block in which to declare local variables.
  • (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);
    Now imagine using it like:
    if (blah == 2)         FOO(blah);
    This interprets to:
    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);
    Which is exactly what you want.
  • (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();