title: 2D Matrix allocation on heap in c++
body: <p>I am looking for a way to allocate a 2D (m <em>x</em> n) matrix on the heap where the elements are <strong>consecutive</strong> in memory. Currently I know of two ways to accomplish that:</p>
  <h3>First approach</h3>
  <pre><code>int* M = new int[m * n];
  </code></pre>
  <ul>
  <li>Pro: The size of <code>M</code> can by dynamically determined.</li>
  <li>Con: Indexing of <code>M</code> is a bit of a pain. (<code>M[i * m + j]</code>)</li>
  </ul>
  <h3>Second approach</h3>
  <pre><code>typedef int dim[2];
  dim* M = new dim[n];
  </code></pre>
  <ul>
  <li>Pro: Indexing of <code>M</code> is just how I want it to be.</li>
  <li>Con: The size of the first dimension (m) cannot be set dynamically.</li>
  </ul>
  <h3>Question</h3>
  <p>Is there a way to allocate a 2D matrix dynamically on the heap where I can index the elements with <code>[i][j]</code> <strong>and</strong> the memory allocation is consecutive?</p>
  <p>I know it would make a lot of sense to use a class for that, but I am specifically looking for a way as described above.</p>