title: How does malloc and free work with errors?
body: <p>I'm just learning about malloc() and free() in C and stumbled upon an important question which I couldn't happen to find anywhere. As an example let's take this simple code snippet:</p>
  <pre><code>    int main(int argc, char  *argv[])
  {
      char *test = malloc(sizeof(char) * 30);
      strcpy(test, &quot;test&quot;);
  
      FILE *file = fopen(&quot;filename&quot;, &quot;r&quot;);
      if (file == NULL){
          error(&quot;Couldn't open file&quot;);
      }
      free(test);
  
     ...
  }
  </code></pre>
  <p>How does <code>malloc()</code> and <code>free()</code> now work with this error which just calls <code>exit(EXIT_FAILURE)</code>. Because the code doesn't reach <code>free(test)</code> Are there problems if an error occurs and the String test isn't freed? Or does it free automatically when the programm exits?</p>