title: Getting bad_array_new_length error when trying to load a text file into a dynamically allocated 2d array
body: <p>So the issue I'm having with this code is that when I run it, I get the error: <code>terminate called after throwing an instance of "std:bad_array_new_length" what(): std:: bad_array_new_length</code> There are no errors otherwise and the code compiles just fine. Below is the text file I'm trying to load:</p>
  
  <pre>
  10  8
  0   255 255 255 0   0   255 255 255 0
  255 0   255 255 0   0   255 255 0   255
  255 255 0   255 255 255 255 0   255 255
  255 255 255 0   255 255 0   255 255 255
  255 255 255 355 0   0   255 255 255 255
  255 255 255 255 0   0   255 255 255 255
  255 255 255 0   255 255 0   255 255 255
  0   0   0   255 255 255 255 0   0   0
  </pre>
  
  <p>The first two values (10/8) are the length(rows) and height(columns) and the rest are meant to be stored in the 2d array.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include "image.h" // Has the prototypes for the functions
  
  using namespace std;
  
  int** load(string imageFile, int &amp;length, int &amp;height) {
      int** array = new int*[length];
      ifstream file(imageFile);
      if(file.is_open()) {
          file &gt;&gt; length; // Takes the first value and stores it as length
          file &gt;&gt; height; // Takes the second value and stores it as height
          for(int i = 0; i &lt; length; i++) {
              array[i] = new int[height]; 
              for(int j = 0; j &lt; height; j++) {
                  file &gt;&gt; array[i][j];
              }
          }
      }
      else {
          cout &lt;&lt; "Unable to open file." &lt;&lt; endl;
          return 0;
      }
      return array;
  }
  
  int main() {
      int height, length;
      int **image = load("../resource/imagecorrupted.txt", length, height);
  }
  </code></pre>
  
  <p>I should add that this is for a homework assignment, so I'm therefor pretty restricted in the things I can do. In main I do need to store the values from load into **image, as well as the function parameters being set out for me, and are unchangeable. Also, it's still pretty early on in the class so for this assignment we aren't expected to use structs or classes or anything like that. Thanks for any help/advice you can give me!</p>
  
  <p>EDIT:</p>
  
  <p>Thanks to @IgorTandetnik talking me through my issue, I found the fix. This new code works, should anyone have the same issue in the future and needs help:</p>
  
  <pre><code>int** load(string imageFile, int &amp;length, int &amp;height) {
      ifstream file(imageFile);
      if(file.is_open()) {
          file &gt;&gt; length; 
          int** array = new int*[length];
          file &gt;&gt; height;
          for(int i = 0; i &lt; length; i++) {
              array[i] = new int[height]; 
              for(int j = 0; j &lt; height; j++) {
                  file &gt;&gt; array[i][j];
              }
           }
           return array;
      }
      else {
          cout &lt;&lt; "Unable to open file." &lt;&lt; endl;
          return 0;
      }
  }
  
  int main() {
      int height = 0;
      int length = 0;
      int **image = load("../resource/imagecorrupted.txt", length, height);
  }
  </code></pre>