title: Benefits of contiguous memory allocation
body: <p>In terms of performance, what are the benefits of allocating a contiguous memory block versus separate memory blocks for a matrix? I.e., instead of writing code like this:</p>
  
  <pre><code>char **matrix = malloc(sizeof(char *) * 50);
  for(i = 0; i &lt; 50; i++)
      matrix[i] = malloc(50);
  </code></pre>
  
  <p>giving me 50 disparate blocks of 50 bytes each and one block of 50 pointers, if I were to instead write:</p>
  
  <pre><code>char **matrix = malloc(sizeof(char *) * 50 + 50 * 50);
  char *data = matrix + sizeof(char *) * 50;
  for(i = 0; i &lt; 50; i++) {
      matrix[i] = data;
      data += 50;
  }
  </code></pre>
  
  <p>giving me one contiguous block of data, what would the benefits be? Avoiding cache misses is the only thing I can think of, and even that's only for small amounts of data (small enough to fit on the cache), right? I've tested this on a small application and have noticed a small speed-up and was wondering why.</p>