title: C - Off by one error, but no segmentation fault?
body: <p>I recently wrote this code in C:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define N_ROWS 100
  
  
  int main() {
  
    char *inputFileName = "triangle_data.txt";
    FILE *inputFile = fopen(inputFileName, "r");
  
    if (inputFile == NULL) {
      printf("ERROR: Failed to open \"%s\".\n", inputFileName);
      return -1;
    }
  
    int triangle[(N_ROWS*(N_ROWS+1))/2 - 1];
    size_t size = sizeof(triangle)/sizeof(int);
    size_t index;
  
  
    for (index = 0; !feof(inputFile); ++index) {
      fscanf(inputFile, "%d", &amp;triangle[index]);
    }
  
    return 1;
  }
  </code></pre>
  
  <p>and was expecting a <code>Segmentation Fault</code>, since <code>N_ROWS*(N_ROWS+1))/2</code> is just enough space to hold the data in the file, but as you can see I made the array one element smaller. Somehow this doesn't trigger a segmentation fault. It does if I replace the body of the <code>for</code>-loop with:</p>
  
  <pre><code>int tmp;
  fscanf(inputFile, "%d", &amp;tmp);
  triangle[index] = tmp;
  </code></pre>
  
  <p>What is happening here. If I make the array three elements to small it still doesn't trigger a segmentation fault. Five elements to small trigger one. I'm sure there is enough data in the file. </p>
  
  <p>As a test I printed the array afterwards and if I choose a smaller array there were elements missing.</p>
  
  <p>What is happening here?</p>
  
  <p>PS: Compiled with <code>clang</code> on a OS X.</p>