1. do {} while (0) in the source code)
Write a code today:
if(!CCScene::init()){return false;}
Later I went to the source code:
bool CCScene::init(){ bool bRet = false; do { CCDirector * pDirector; CC_BREAK_IF( ! (pDirector = CCDirector::sharedDirector()) ); this->setContentSize(pDirector->getWinSize()); // success bRet = true; } while (0); return bRet;}
After reading it, I wonder, do {} while (0) is not a single execution. What does it mean?
Later, Baidu got the following best explanation:
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:
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 wocould 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 wocould 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 beIf-Statement with two branches:
if (x > y) exch(x,y); // Branch 1else do_something(); // Branch 2
But it wocould be interpreted asIf-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 statementelse // 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 blockDoAndWhile (0). Then we have a single statement with the capabilities of a block, but not considered as being a block statement by the compiler. OurIf-Statement now becomes:
if (x > y) do { int tmp; tmp = x; x = y; y = tmp; } while(0);else do_something();
In general, the purpose of nesting is fixed when the Code uses a macro definition.
Although the above Code is not well reflected, most macro definitions in the source code use the do {} while (0) structure:
#define CC_SAFE_DELETE(p) do { if(p) { delete (p); (p) = 0; } } while(0)#define CC_SAFE_DELETE_ARRAY(p) do { if(p) { delete[] (p); (p) = 0; } } while(0)#define CC_SAFE_FREE(p) do { if(p) { free(p); (p) = 0; } } while(0)#define CC_SAFE_RELEASE(p) do { if(p) { (p)->release(); } } while(0)#define CC_SAFE_RELEASE_NULL(p) do { if(p) { (p)->release(); (p) = 0; } } while(0)#define CC_SAFE_RETAIN(p) do { if(p) { (p)->retain(); } } while(0)