title: C - insert lines from very big file into array
body: <p>First of all, I'm quite new with C, and I know this is a very repeated question, however, I could not find anything that could help me with my problem.</p>
  
  <p>Here is my code:
  It takes a text file and stores each line in an array.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main()
  {
      FILE *file;
      file = fopen("test.txt", "r");
      char buffer[600];
      char *lines[10000];
      int i = 0;
  
      while(fgets(buffer, sizeof(buffer), file))
      {
  
          lines[i] = malloc(sizeof(buffer));
          strcpy(lines[i], buffer);
          i++;
          free(lines[i]);
      }
  
      fclose(file);
  
  
      return 1;
  }
  
  </code></pre>
  
  <p>This works fine for small text files.</p>
  
  <p>However it doesn't with large ones (even setting buffer and lines with much bigger numbers). Actually, if I increment buffer[] and *lines[] like 1000000 bytes, it doesn't give anything (if I understood well, it gives undefined behaviour). And I need to get this work with a 100.000 lines file with variable length lines,</p>
  
  <p>So, how could I declare a very large array so I can pass each line? Since, as I exposed, it doesn't work with a large file.</p>
  
  <p>Any help is appreciated!</p>