delete multidimensional arrays
<p>In C++ FAQ, the [16.16] gives the following example,</p>

<pre><code>void manipulateArray(unsigned nrows, unsigned ncols[])
{
   typedef Fred* FredPtr;
   FredPtr* matrix = new FredPtr[nrows];

   // Set each element to NULL in case there is an exception later.
   // (See comments at the top of the try block for rationale.)
   for (unsigned i = 0; i &lt; nrows; ++i)
       matrix[i] = NULL;
   try {
     for (unsigned i = 0; i &lt; nrows; ++i)
     matrix[i] = new Fred[ ncols[i] ];
     for (unsigned i = 0; i &lt; nrows; ++i) {
        for (unsigned j = 0; j &lt; ncols[i]; ++j) {
         someFunction( matrix[i][j] );
         }
      }

     if (today == "Tuesday" &amp;&amp; moon.isFull()) {
       for (unsigned i = nrows; i &gt; 0; --i)
        delete[] matrix[i-1];
       delete[] matrix;
       return;
     }

 ...code that fiddles with the matrix...

    }
  catch (...) {
     for (unsigned i = nrows; i &gt; 0; --i)
      delete[] matrix[i-1];
      delete[] matrix;
       throw;    // Re-throw the current exception
   }

  for (unsigned i = nrows; i &gt; 0; --i)
     delete[] matrix[i-1];
     delete[] matrix;
  }
</code></pre>

<p>Why we have to use delete this way, I mean, </p>

<p>First      <code>delete[] matrix[i-1];</code> 
then     <code>delete[] matrix;</code></p>

<p>Moreover, what’s the point of after the whole “try…catch” cycle, we still have to put</p>

<pre><code>for (unsigned i = nrows; i &gt; 0; --i)
   delete[] matrix[i-1];
   delete[] matrix;
</code></pre>

<p>at the end of this function.</p>
