Note that there are some explanatory texts on larger screens.

plurals
  1. POC: Nested Ifs or Gotos
    primarykey
    data
    text
    <p>What is the best way to manage resources for a <strong>C</strong> program. Should I use a nested if structure or should I use goto statements? </p> <p>I am aware there is a lot of <strong>taboo</strong> about <strong>goto</strong> statements. However, I think it is justified for local resource clean up. I have supplied two samples. One compares a nested if structure and another uses goto statements. I personally find the goto statements make the code easier to read. For those who might argue that the <strong>nested if</strong> prompt better structure, imagine if the datatype was something other than a char*, like a Windows handle. I feel that the <strong>nested if</strong> structure would get out of hand with a series of <strong><a href="http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx" rel="nofollow noreferrer">CreateFile</a></strong> functions or any other function that takes large quantities of parameters.</p> <p>This <a href="http://vilimpoc.org/research/raii-in-c/" rel="nofollow noreferrer">article</a> demonstrates that local goto statements create RAII for C code. The code is neat an easy to follow. Imagine that as a series of <strong>nested if</strong> statements. </p> <p>I understand that <strong>goto</strong> is taboo in many other languages because their exists other control mechanisms like try/catch etc, however, in C it seems appropriate.</p> <pre><code>#include &lt;stdlib.h&gt; #define STRING_MAX 10 void gotoExample() { char *string1, *string2, *string3, *string4, *string5; if ( !(string1 = (char*) calloc(STRING_MAX, sizeof(char))) ) goto gotoExample_string1; if ( !(string2 = (char*) calloc(STRING_MAX, sizeof(char))) ) goto gotoExample_string2; if ( !(string3 = (char*) calloc(STRING_MAX, sizeof(char))) ) goto gotoExample_string3; if ( !(string4 = (char*) calloc(STRING_MAX, sizeof(char))) ) goto gotoExample_string4; if ( !(string5 = (char*) calloc(STRING_MAX, sizeof(char))) ) goto gotoExample_string5; //important code goes here gotoExample_string5: free(string4); gotoExample_string4: free(string3); gotoExample_string3: free(string2); gotoExample_string2: free(string1); gotoExample_string1: } void nestedIfExample() { char *string1, *string2, *string3, *string4, *string5; if (string1 = (char*) calloc(STRING_MAX, sizeof(char))) { if (string2 = (char*) calloc(STRING_MAX, sizeof(char))) { if (string3 = (char*) calloc(STRING_MAX, sizeof(char))) { if (string4 = (char*) calloc(STRING_MAX, sizeof(char))) { if (string5 = (char*) calloc(STRING_MAX, sizeof(char))) { //important code here free(string5); } free(string4); } free(string3); } free(string2); } free(string1); } } int main(int argc, char* argv[]) { nestedIfExample(); gotoExample(); return 0; } </code></pre> <p>I would also like to quote Linus Torvalds on <strong>goto</strong> statements inside the Linux <a href="http://kerneltrap.org/node/553/2131" rel="nofollow noreferrer">Kernel</a>.</p> <blockquote> <p>And sometimes structure is <em>bad</em>, and gets into the way, and using a "goto" is just much clearer.</p> <p>For example, it is quite common to have conditionals THAT DO NOT NEST.</p> <p>In which case you have two possibilities</p> <ul> <li><p>use goto, and be happy, since it doesn't enforce nesting</p> <p>This makes the code <em>more</em> readable, since the code just does what the algorithm says it should do.</p></li> <li><p>duplicate the code, and rewrite it in a nesting form so that you can<br> use the structured jumps.</p> <p>This often makes the code much LESS readable, harder to maintain, and bigger.</p></li> </ul> <p>The Pascal language is a prime example of the latter problem. Because it doesn't have a "break" statement, loops in (traditional) Pascal end up often looking like total shit, because you have to add totally arbitrary logic to say "I'm done now".</p> </blockquote> <p>Is <strong>goto</strong> acceptable for resource management? Should I use <strong>nested if</strong> statements or is there a better way?</p> <p><strong>Update:</strong> <a href="https://stackoverflow.com/questions/245742/examples-of-good-gotos-in-c-or-c">Examples of Good Gotos In C</a></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.
 

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