Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there any other method to handle many 'malloc' failures?
    primarykey
    data
    text
    <p>I'm trying to write a function in C to solve a math problem. In that function, there are several steps, and each step needs to allocate some memory with the size depending on the calculation results in previous steps (so I can't allocate them all at the beginning of the function). The pseudo code looks like:</p> <pre><code>int func(){ int *p1, *p2, *p3, *p4; ... p1 = malloc(...); if(!p1){ return -1; //fail in step 1 } ... p2 = malloc(...); if(!p2){ free(p1); return -2; //fail in step 2 } ... p3 = malloc(...); if(!p3){ free(p1); free(p2); return -3; //fail in step 3 } ... p4 = malloc(...); if(!p4){ free(p1); free(p2); free(p3); /* I have to write too many "free"s here! */ return -4; //fail in step 4 } ... free(p1); free(p2); free(p3); free(p4); return 0; //normal exit } </code></pre> <p>The above way to handle malloc failures is so ugly. Thus, I do it in the following way:</p> <pre><code>int func(){ int *p1=NULL, *p2=NULL, *p3=NULL, *p4=NULL; int retCode=0; ... /* other "malloc"s and "if" blocks here */ ... p3 = malloc(...); if(!p3){ retCode = -3; //fail in step 3 goto FREE_ALL_EXIT; } ... p4 = malloc(...); if(!p4){ retCode = -4; //fail in step 4 goto FREE_ALL_EXIT; } ... FREE_ALL_EXIT: free(p1); free(p2); free(p3); free(p4); return retCode; //normal exit } </code></pre> <p>Although I believe it's more brief, clear, and beautiful now, my team mate is still strongly against the use of 'goto'. And he suggested the following method:</p> <pre><code>int func(){ int *p1=NULL, *p2=NULL, *p3=NULL, *p4=NULL; int retCode=0; ... do{ /* other "malloc"s and "if" blocks here */ p4 = malloc(...); if(!p4){ retCode = -4; //fail in step 4 break; } ... }while(0); free(p1); free(p2); free(p3); free(p4); return retCode; //normal exit } </code></pre> <p>Hmmm, it seems a way to avoid the use of 'goto', but this way increases indents, which makes the code ugly.</p> <p><strong>So my question is,</strong> is there any other method to handle many 'malloc' failures in a good code style? Thank you all.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload