title: C - How to Read String Lines from Stdin or File Memory Save
body: <p>I need a version of read line that is memory save. I have this "working" solution. But I'm not sure how it behaves with memory. When I enable <code>free(text)</code> it works for a few lines and then I get an error. So now neither text nor result is ever freed although I malloc text. Is that correct ? And why is that so ?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  char* readFromIn()
  {
      char* text = malloc(1024);
      char* result = fgets(text, 1024, stdin);
      if (result[strlen(result) - 1] == 10)
          result[strlen(result) - 1] = 0;
      //free(text);
      return result;
  }
  </code></pre>
  
  <p>I have <strong>A LOT</strong> of short lines to read with this and I also need stdin to be replaceable with a <code>FILE*</code> handle. There is no need for me to realloc text because I have only short lines.</p>