DoWhile0--华硕笔试题
2011-10-17 20:54
92 查看
华硕2012校招碰到的一道题,之前在实验室讨论过,关于Linux内核里很多的宏定义都是用do{}while(0)写的,讨论没那么深入,刚好http://kernelnewbies.org/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).
空的宏定义,编译器会给warning。
(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:
Now imagine using it like:
This interprets to:
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:
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:
However that wouldn't work in some cases. The following code is meant to be an if-statement with two branches:
But it would be interpreted as an if-statement with only one branch:
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:
(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.
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).
空的宏定义,编译器会给warning。
(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 like do { ... } 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();
(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.
#define FOO(arg) ({ \ typeof(arg) lcl; \ lcl = bar(arg); \ lcl; \ })
相关文章推荐
- 几道有关字符串处理的编程题(华硕,盛大招聘笔试题)
- 华硕笔试: 基础知识
- JAVA笔试试题(一)
- 华硕笔记本更换操作系统的一波三折的遭遇
- 阿里巴巴2014实习生招聘研发工程师笔试题
- 微软笔试题4:Buiding in Sandbox
- 2017.8.1拼多多内推内推笔试题(1)—最大乘积的求取(分治求K大(小))
- 华科10年计算机考研复试笔试(算法基础)(1)
- 微软2013年笔试题详解及深入
- 苏宁面试题,笔试题回顾
- Java笔试题分类总结--基本数据类型
- 网易2017春招笔试真题编程题集合--工作安排
- 淘宝笔试的一道题
- sql笔试题(in和exits区别)
- 【搜狗笔试题】给定一个数组a[N],我们希望构造数组b[N]
- 蘑菇街笔试题 投篮比赛
- 笔试题 1
- .Net面试题汇总(三) 帮你轻松过笔试关 --不断增加中
- 网易2011年笔试
- 笔试中常用的计算公式,排列与组合