title: Difference between array type and array allocated with malloc
body: <p>Today I was helping a friend of mine with some C code, and I've found some strange behavior that I couldn't explain him why it was happening. We had TSV file with a list of integers, with an <code>int</code> each line. The first line was the number of lines the list had.</p>
  <p>We also had a c file with a very simple &quot;readfile&quot;. The first line was read to <code>n</code>, the number of lines, then there was an initialization of:</p>
  <pre><code>int list[n]
  </code></pre>
  <p>and finally a for loop of <code>n</code> with a <code>fscanf</code>.</p>
  <p>For small n's (till ~100.000), everything was fine. However, we've found that when n was big (10^6), a segfault would occur.</p>
  <p>Finally, we changed the list initialization to</p>
  <pre><code>int *list = malloc(n*sizeof(int))
  </code></pre>
  <p>and everything when well, even with very large <code>n</code>.</p>
  <p>Can someone explain why this occurred? what was causing the segfault with <code>int list[n]</code>, that was stopped when we start using <code>list = malloc(n*sizeof(int))</code>?</p>