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