do-while(0)
大约 3 分钟
do-while(0)
cocos2d-x
代码中
为了代码分块,比仅仅使用{}更直观些。例如在do
{
CCImage* pImage = new CCImage();
CC_BREAK_IF(NULL == pImage);
bRet = pImage->initWithString(text, (int)dimensions.width, (int)dimensions.height, eAlign, fontName, (int)fontSize);
CC_BREAK_IF(!bRet);
bRet = initWithImage(pImage);
CC_SAFE_RELEASE(pImage);
} while (0);
展开的时候不会出错
不使用{}把action1()、action2()包起来
假设你需要定义这样一个宏:
#define DOSOMETHING() action1(); action2();
这个宏的本意是,当执行DOSOMETHING()时,action1(),action2()都会被调用。如果有判断,再执行这个宏的话,如下:
if(NULL == pPointer)
DOSOMETHING();
else
...
这样宏在预处理的时候会直接被展开,放在花括号里,那么实际上写的代码如下:
if(NULL == pPointer)
action1();
action2();
else
...
这展开存在两个问题:
- 假设没有else分支,则DOSOMETHING中的第二个语句无论if测试是否通过,都会执行。
- 因为if分支后面有两个语句,导致else分支没有对应的if,编译失败。
?比如: 使用{}把action1()、action2()包起来
#define DOSOMETHING() { action1(); action2(); }
我们在写代码的时候都习惯在语句右面加上分号,如果在宏中使用{},代码编译展开后宏就相当于这样写了:{...};
,展开后如下:
if(NULL == pPointer)
{
action1();
action2();
}; //大括号后多了一个分号
else
...
这段代码中大括号后多了一个分号,如果有else,那么else又没有对应的if了,编译出错。
do{...}while(0)
来定义宏
使用#define DOSOMETHING() \
do{ \
action1();\
action2();\
}while(0)\
用到空宏
避免由宏引起的警告 内核中由于不同架构的限制,很多时候会用到空宏。在编译的时候,这些空宏会给出警告,为了避免这样的warning,我们可以使用do{...}while(0)
来定义空宏:
#define DOSOMETHING() do{}while(0)
我想跳到另外一段代码了,剩下的不执行了
- 当你执行一段代码到一半,想跳过剩下的一半的时候,如果你正处于
do{...}while(0)
循环中,则能用break达到这个目的。
do
{
执行.
再执行…
if (如果有什么条件满足)
{
我想跳到另外一段代码了,剩下的不执行了,可是不建议用goto语句,怎么办呢?
break;/*搞定*/
}
我有可能被执行.
}while(false)
举个例子如下
do
{
if(!a) break;
//do something here
if(!b) break;
//do another thing here
}while(0);
- 变形的goto,有些公司不让用goto。在一些函数中,需要实现条件转移,或者构成循环,跳出循环体,使用goto总是一种简单的方法,例如:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str;
/* 最初的内存分配 */
str = (char *) malloc(15);
if(str != NULL)
goto loop;
printf("hello world\n");
loop:
printf("malloc success\n");
return(0);
}
但由于goto不符合软件工程的结构化,而且有可能使得代码难懂,所以很多人都不倡导使用,这个时候我们可以使用do{...}while(0)
来做同样的事情:
#include <stdio.h>
#include <stdlib.h>
int main()
{
do{
char *str;
/* 最初的内存分配 */
str = (char *) malloc(15);
if(str != NULL)
break;
printf("hello world\n");
}while(0);
printf("malloc success\n");
return(0);
}