title: How to dynamically allocate a contiguous block of memory for a 2D array
body: <p>If I allocate a 2D array like this <strong>int a[N][N]</strong>; it will allocate a contiguous block of memory.</p>
  
  <p>But if I try to do it dynamically like this : </p>
  
  <pre><code>int **a = malloc(rows * sizeof(int*));
  for(int i = 0; i &lt; rows; i++) 
     a[i] = malloc(cols * sizeof(int));
  </code></pre>
  
  <p>This maintains a unit stride between the elements in the rows, but this may not be the case between rows.</p>
  
  <p>One solution is to convert from 2D to 1D, besides that, is there another way to do it?</p>