The magical do{}while (0)

Source: Internet
Author: User
Tags error handling goto

Author: cpoint Source: http://www.cnblogs.com/cpoint/

In the Linux kernel code, often see Do...while (0) of the macro, Do...while (0) has a lot of effect, the following list of several: 1, avoid goto statement:

Typically, if a function starts to allocate some resources and then exits the function if it encounters an error halfway, of course, to release resources before exiting, our code might look like this:

1 #defien N
 2 
 3 bool Execute ()
 4 {
 5    //Allocated resource
 6    int *p = (int *) malloc (N * sizeof (int)); 
  
   7    bool bOk = true;
 8 
 9    //execution and error handling    . bOk = Func1 ();
One    if (!bok) 
(       p);       p = NULL;       return false;    () + bOk = Func2 ();    if (!bok) (       p);       p = NULL;       return to false;
+    bOk = func3 ();    if (!bok) (       p);       p = NULL;       return false;
* * ..... .....    The success of the implementation, the release of resources and the return of the
PNS free     (p);     p = NULL;
The     return is true;
40}
  

The biggest problem here is code redundancy, each additional operation, it is necessary to do the corresponding error handling, very inflexible, so think of a goto:

1 #defien N
 2 
 3 bool Execute ()
 4 {
 5    //Allocated resource
 6    int *p = (int *) malloc (N * sizeof (int)); 
  
   7    bool bOk = true;
 8 
 9    //execution and error handling    . bOk = Func1 ();    if (!bok) goto Errorhandle;    bOk = Func2 ();    if (!bok) goto Errorhandle;    bOk = func3 ();    if (!bok) goto Errorhandle;
* 
/    /...
...    The success of the implementation, the release of resources and return to the free     (p);     p = NULL;     return true;     errorhandle: Free     (p);     p = NULL;     return false; 
30}
  

Code redundancy is solved, but introduces the more subtle goto statements in C, although the correct use of the Goto statement can greatly improve the flexibility and simplicity of the program, but will make our program unpredictable, in order to avoid the use of Goto statement, but also to eliminate the code redundancy, you can consider using the following do ... while (0):

1 #defien N
 2 
 3 bool Execute ()
 4 {
 5     //Allocated resource
 6     int *p = (int *) malloc (N * sizeof (int));
 7     bool BOK = true;
 8 
 9     do {One         -to-one//execute and error handling         . BOK = Fun1 ();         if (!bok) break;         BOK = fun2 ();         if (!bok) break;
+         BOK = Fun3 ();         if (!bok) break;
*         //...
..  (0);     Release Resources     (p);     p = NULL;
-     return BOK;
29}
2. Avoid null declarations at compile time warning:

In the Linux kernel source code, you often see the following macros to avoid warnings at compile time:

#define FOO do {} and (0)
3. Provide a base block for declaring local variables:

You may often use the following macros:

#define EXCH (x, y) {int tmp; tmp=x; x=y; y=tmp;}

However, in some cases it will fail, the code below uses If...else ...

if (x > Y)
        Exch (y);          Branch 1
Else  
        do_something ();     Branch 2

But the if statement that will be interpreted as a branch:

if (x > Y) {     
        int tmp;            
        TMP = x;            
        x = y;
        y = tmp;
}
;                           Empty statement
Else                        //ERROR!!! 
        Do_something ();

The error is in the ";" Directly behind the code block, the workaround is to embed the code in Do...while (0) and get the following code:

1 if (x > Y)
2 do         {
3                 int tmp;
4                 tmp = x;
5                 x = y;
6                 y = tmp;
7         } while (0);
8 Else
9         do_something ();

So the above macro can be modified to:

1 #define EXCH (x, y) do       {\
2                 int tmp;\
3                 tmp = x;\
4                 x =

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.