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:

       
       
    1. #define FOO(x) /
    2.         printf("arg is %s/n", x); /
    3.         do_something_useful(x);

    Now imagine using it like:
       
       
    1. if (blah == 2)
    2.         FOO(blah);

    This interprets to:
       
       
    1. if (blah == 2)
    2.         printf("arg is %s/n", blah);
    3.         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 like do { ... } while(0), you would get this:

       
       
    1. if (blah == 2)
    2.         do {
    3.                 printf("arg is %s/n", blah);
    4.                 do_something_useful(blah);
    5.         } 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:

       
       
    1. #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:

       
       
    1. if (x > y)
    2.         exch(x,y);          // Branch 1
    3. else  
    4.         do_something();     // Branch 2

    But it would be interpreted as an if-statement with only one branch:

       
       
    1. if (x > y) {                // Single-branch if-statement!!!
    2.         int tmp;            // The one and only branch consists
    3.         tmp = x;            // of the block.
    4.         x = y;
    5.         y = tmp;
    6. }
    7. ;                           // empty statement
    8. else                        // ERROR!!! "parse error before else"
    9.         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:

       
       
    1. if (x > y)
    2.         do {
    3.                 int tmp;
    4.                 tmp = x;
    5.                 x = y;
    6.                 y = tmp;
    7.         } while(0);
    8. else
    9.         do_something();

  • (from Bart Trojanowski) gcc adds Statement-Expressions which provide an alternative to the do-while-0 block. They provide the above mentioned benefits and are slightly more legible.

       
       
    1. #define FOO(arg) ({         /
    2.            typeof(arg) lcl; /
    3.            lcl = bar(arg);  /
    4.            lcl;             /
    5.     })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值