The magical application of Do...while (0) in C language-Avoid Goto

Source: Internet
Author: User

Use goto elegance and avoid structural confusion
The statement you want to jump to with do{...} while (0) you can wrap it up.


Reference

 #defien N BOOLExecute () {//Allocate resources    int*p = (int*)mallocNsizeof(int));BOOLBOk =true;//Run and error handlingBOk = Func1 ();if(!bok) { Free(p); p = NULL;return false; } bOk = Func2 ();if(!bok) { Free(p); p = NULL;return false; } bOk = Func3 ();if(!bok) { Free(p); p = NULL;return false; }// ..........    //Run successfully, release resources and return      Free(p); p = NULL;return true; }
#defien N BOOLExecute () {//Allocate resources    int*p = (int*)mallocNsizeof(int));BOOLBOk =true;//Run and error handlingBOk = Func1 ();if(!bok)GotoErrorhandle; BOk = Func2 ();if(!bok)GotoErrorhandle; BOk = Func3 ();if(!bok)GotoErrorhandle;// ..........    //Run successfully, release resources and return      Free(p); p = NULL;return true; Errorhandle: Free(p); p = NULL;return false; }
#defien N BOOLExecute () {//Allocate resources     int*p = (int*)mallocNsizeof(int));BOOLBOK =true; Do{//Run and error handlingBOK = Fun1 ();if(!bok) Break; BOK = Fun2 ();if(!bok) Break; BOK = Fun3 ();if(!bok) Break;//.........} while(0);//Release Resources      Free(p); p = NULL;returnBOK; }

The magical application of Do...while (0) in C language-Avoid Goto

Related Article

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.