title: 2D array error: Vector index beyond memory allocation
body: <p>can someone help me understand why i'm getting this error.</p>
  
  <p>I have a file with numbers
  5 4<br>
  9 1 5 3<br>
  14 12 3 10<br>
  9 7 10 14<br>
  8 5 0 3<br>
  14 13 6 14<br>
  8 11<br></p>
  
  <p>I am adding these numbers to a 2D vector:</p>
  
  <pre><code> 25 bool function(const char* myfile){
   26 
   27   std::vector&lt; std::vector&lt;int&gt; &gt; data;
   28 
   29   std::ifstream file(myfile);
   30   std::string line;
   31 
   32   while(std::getline(file, line)){
   33     std::vector&lt;int&gt; lineData;
   34     std::stringstream linestream(line);
   35 
   36     int value;
   37     while(linestream &gt;&gt; value)
   38     {
   39       lineData.push_back(value);
   40     }
   41     data.push_back(lineData);
   42   }
   43 
   44   int i, j = 0;
   45   int vertexSize = 0;
   46   std::vector&lt; std::vector&lt;int&gt; &gt;::iterator row;
   47   std::vector&lt;int&gt;::iterator col;
   48   for( row = data.begin(); row != data.end(); row++, i++){
   49     for(col = row-&gt;begin(); col!= row-&gt;end(); col++){
   50       std::cout &lt;&lt; *col &lt;&lt; " ";
   51     }
   52     std::cout &lt;&lt; "\n";
   53   }
   54 
   55    vertexSize = data[0][0] * data[0][1];
   56    start = data[i-1][0];
   57    goal = data[i-1][1];
   58 
   59    std::cout &lt;&lt; "Vertex Size:" &lt;&lt; vertexSize &lt;&lt; "\n";
   60    std::cout &lt;&lt; "Start: " &lt;&lt; start &lt;&lt; " goal:" &lt;&lt; goal &lt;&lt; "\n";
   61   return true;
   62 }
  </code></pre>
  
  <p>When i try to get the last 2 numbers in the last line, i am getting the error:</p>
  
  <pre><code>5 4 
  9 1 5 3 
  14 12 3 10 
  9 7 10 14 
  8 5 0 3 
  14 13 6 14 
  8 11 
  Vertex Size:20
  Start: 8 goal:7
  ** Vector&lt;T&gt;::operator[] error: vector index beyond memory allocation!
     Unable to recover, no memory allocated
     Terminating program
  </code></pre>
  
  <p>std::cout &lt;&lt; data[i-1].size(); Shows 2 elements, which is what I am expecting, yet it is still giving me that index beyond memory allocation error for goal.</p>
  
  <p>It seems that if i go beyond data[number][0], that is when the errors occur.
  Can someone explain to me why that is happening?</p>
  
  <p>Thank you for your help.</p>
  
  <p>Debugging with gdb:</p>
  
  <pre><code>warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000
  5 4 
  9 1 5 3 
  14 12 3 10 
  9 7 10 14 
  8 5 0 3 
  14 13 6 14 
  8 11 
  0 0
  Vertex Size:20
  Start: 8 goal:7
  ** Vector&lt;T&gt;::operator[] error: vector index beyond memory allocation!
     Unable to recover, no memory allocated
     Terminating program
  
  Program exited with code 01.
  (gdb) backtrace
  No stack.
  </code></pre>