[C ++ basics 03] do... while (0), 03do... while

Source: Internet
Author: User

[C ++ basics 03] do... while (0), 03do... while

My topic is that sometimes knowing some details will help you write better code.

========================================================== ====

Before learning coocs2d-x, found a lot of do... the while (0) statement does not understand why it was written at the beginning. (it does not play the role of a loop.) then, I found that it is quite useful, here we will summarize:

Do... the magic of while (0.

1. Avoid using goto

For example, we need to handle some errors in the function, and exit the function if an error occurs. Of course, we need to release the resources before exiting, as shown below:

Bool HelloWorld: init () {// allocate resource int * p = new int; bool bRet = true; // execute and handle errors bRet = func1 (); if (! BRet) {delete p; p = NULL; return false;} bRet = func2 (); if (! BRet) {delete p; p = NULL; return false;} bRet = func3 (); if (! BRet) {delete p; p = NULL; return false ;}//.......... // The execution is successful. The resource is released and delete p; p = NULL; return true;} is returned ;}

In this way, the biggest problem of writing is generation Code redundancy. Each time an operation is added, it must be processed once, not flexible. So now we think of goto, and the above version becomes as follows:

Bool HelloWorld: init () {// allocate resource int * p = new int; bool bRet = true; // execute and handle errors bRet = func1 (); if (! BRet) goto error; bRet = func2 (); if (! BRet) goto error; bRet = func3 (); if (! BRet) goto error; // The execution is successful. After resources are released, delete p; p = NULL; return true; error: delete p; p = NULL; return false;} is returned ;}
Of course, Although the goto is flexible and convenient, it is very dangerous (this is the same as the red face and water. Why did you talk about it ?) Therefore, most books recommend that you do not use this item in programs as much as possible.So we can use do... while to eliminate it. As follows:

Bool HelloWorld: init () {// allocate resource int * p = new int; bool bRet = true; do {// execute and handle errors bRet = func1 (); if (! BRet) break; bRet = func2 (); if (! BRet) break; bRet = func3 (); if (! BRet) break;} while (0); // The execution is successful. The resource is released and delete p is returned. p = NULL; return bRet ;}

2. do... while (0) in macro definition)


Often we can see the shadow of the goods in the macro definition, such as in the cocos2d-x

#define CC_SAFE_DELETE(p)    do { delete (p); (p) = nullptr; } while(0)


So there is no loop and only one execution. What is the significance of this writing?

Let's remove do... while, as shown below:

# Define CC_SAFE_DELETE (p) delete (p); (p) = nullptr; if (p! = NULL) CC_SAFE_DELETE (p); // The above will be converted to this if (p! = NULL) delete (p); (p) = nullptr;

Above, The second sentence is always executed, Obviously not interesting. Someone may say that if we add a bracket, for example:

# Define CC_SAFE_DELETE (p) {delete (p); (p) = nullptr;} // This situation may occur if (p! = NULL) CC_SAFE_DELETE (p); else // do else… // The above will be converted to this if (p! = NULL) {delete (p); (p) = nullptr ;}; else // do else…

Else compilation error.


Maybe you will say that the habit of our code is to add {} after each judgment, so there will be no such problem, so do... while is not required, such:

if(...) {}else{}

It is true that this is a good programming habit that should be promoted, But what we need to do is to make the code universal and strong, so we advocate the use of do... while (0.

==========================================================


Reprinted please indicate the source: http://blog.csdn.net/shun_fzll/article/details/37776429




Logic in C language or usage in dowhile

I don't know what you are talking about. First you enter-1, the screen prints-1, and then judge that a <0 is true and continues to run. If you input 2, the screen prints 2, then judge that a is not true and the program ends.

Do while usage in C ++

Run immediately
While (! = B)
{
//......
}
When B is not equal to a, execute
If yes
While (a = B)
It is executed when a is equal to B.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.