title: C++ bad_array_new_length exception thrown by return statement?
body: <p>I've been making a Matrix class over the course of the week and have run into a problem that has left me stumped: the return statement of one of my functions is throwing a bad_array_new_length exception!</p>
  
  <p>Here's example code that I used to find this out:</p>
  
  <pre><code>Matrix Matrix::operator*(Matrix&amp; mat)
  {
      if (this-&gt;columns != mat.rows)
      {
          //Do code if Matrix can't be multiplied.
      }
      else
      {
          Matrix result(this-&gt;rows, mat.columns);
  
          //Multiply Matrices together.
  
          //Print out result to prove the local Matrix works fine.
          //It does.
  
          try
          {
              return result;
          }
          catch (const exception&amp; e)
          {
              cout &lt;&lt; "return exception: " &lt;&lt; e.what() &lt;&lt; endl;
              return result;  //To be caught again in the test.
          }
      }
  }
  </code></pre>
  
  <p>Low and behold, when I run the function it prints out "return exception: bad_array_new_length" onto the console.</p>
  
  <p>The function was tested like so:</p>
  
  <pre><code>try
  {
      answer = A1 * B1;   //A1 and B1 are able to be multiplied.
  }
  catch (const exception&amp; e)
  {
      cout &lt;&lt; e.what() &lt;&lt; endl;
  }
  </code></pre>
  
  <p>The test also catches the exception and prints it out as well.</p>
  
  <p>After doing a little research, the bad_array_new_length exception is only thrown when an array is given an invalid length. The Matrix class does use a multidimensional array to store its doubles, but I would think that this isn't the root of the problem because the local Matrix works perfectly as intended.</p>
  
  <p>Here is how the multidimensional array is declared and initialized in the constructor, just in case:</p>
  
  <pre><code>//Matrix.h
  
  unsigned int rows;
  unsigned int columns;
  
  double ** storage = new double*[rows];  //Multidimensional array isn't completely formed, see the constructor.
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  //Matrix.cpp
  
  Matrix::Matrix(unsigned int x, unsigned int y)
  :
      rows(x),
      columns(y)
  {
      for (unsigned int i = 0; i &lt; rows; ++i) //Completes the formation of the multidimensional array.
          storage[i] = new double[columns];
  
      for (unsigned int i = 0; i &lt; rows; ++i)
      {
          for (unsigned int q = 0; q &lt; columns; ++q)
          {
              storage[i][q] = 0;  //Initializes each value in the array as 0.
          }
      }
  }
  </code></pre>
  
  <p>/////////////////////////////////////////////////////////////////////////////////////////////////////</p>
  
  <p>EDIT:</p>
  
  <p>Here is the defined copy constructor and assignment operator:</p>
  
  <pre><code>Matrix::Matrix(const Matrix&amp; obj)
  {
      rows = obj.rows;
      columns = obj.columns;
  
      for (unsigned int i = 0; i &lt; rows; ++i)
      {
          for (unsigned int q = 0; q &lt; columns; ++q)
          {
              storage[i][q] = obj.storage[i][q];
          }
      }
  }
  
  ////////////////////////////////////////////////////////////////////////////////
  
  bool Matrix::operator=(Matrix mat)
  {
      rows = mat.rows;
      columns = mat.columns;
  
      for (unsigned int i = 0; i &lt; rows; ++i)
      {
          for (unsigned int q = 0; q &lt; columns; ++q)
          {
              storage[i][q] = mat.storage[i][q];
          }
      }
  
      return true;
  }
  </code></pre>
  
  <p>Out of curiosity, I changed the code in the test to only be:</p>
  
  <pre><code>try
  {
      A1 * B1;    //Removed assignment to answer Matrix.
  }
  catch (const exception&amp; e)
  {
      cout &lt;&lt; e.what() &lt;&lt; endl;
  }
  </code></pre>
  
  <p>..and the exception still threw as normal.</p>