title: Memory allocation when casting to a VLA type
body: <p>I am puzzled by the memory allocation and accesses to a matrix of pointers in an application I am working in.</p>
  
  <p>There is a matrix defined as:</p>
  
  <pre><code>typedef double (*foo)[n/m][m][m];
  </code></pre>
  
  <p>Which I understand to be a three-dimensional matrix storing pointers to doubles. The memory allocation for all dimensions is done automatically.</p>
  
  <p>Nonetheless, the following appears:</p>
  
  <pre><code>foo bar = (foo) malloc(sizeof(double) * n * n);
  </code></pre>
  
  <p>What exactly is this cast doing?. Furthermore, why do we need to allocate memory for these doubles?. I would think the matrix only contains pointers, which would later be initialized with the memory addresses of doubles declared separately.</p>
  
  <p>Finally, I am, also confused by the way this matrix is accessed during its initialization:</p>
  
  <pre><code>bar[i/m][j/m][i%m][j%m] = value;
  </code></pre>
  
  <p>Where i and j are smaller than N. Mainly, I would like to know what the fourth index is adressing.</p>
  
  <p>Thank you so much in advance for your help!</p>