title: Do I need to call free() after a failed getline()?
body: <p>The following block causes a memory leak:</p>
  
  <pre><code>FILE *fp = fopen(path, "r");
  char *line = NULL;
  size_t len = 0;
  ssize_t read = -1;
  
  while ((read = getline(&amp;line, &amp;len, fp)) != -1) {
          /*Do something*/
  }
  </code></pre>
  
  <blockquote>
    <p>120 bytes in 1 blocks are definitely lost...</p>
    
    <p>...getline (getline.c:34)</p>
  </blockquote>
  
  <p>I can fix this by adding a <code>free()</code>:</p>
  
  <pre><code>while ((read = getline(&amp;line, &amp;len, fp)) != -1) {
          /*Do something*/
  }
  free(line);
  </code></pre>
  
  <p><strong>My question is</strong>: Why does <code>getline</code> allocate memory for <code>line</code> when it fails? And why do I not need to <code>free(line)</code> for every call to <code>getline</code>?</p>