title: C++ free(): invalid next size (fast)
body: <p>I've looked at similar questions (such as <a href="https://stackoverflow.com/questions/4729395/error-free-invalid-next-size-fast">this</a>, <a href="https://stackoverflow.com/questions/25436181/c-error-free-invalid-next-size-fast-different-behaviour-of-c-program-on">this</a>, and <a href="https://stackoverflow.com/questions/13206714/c-error-free-invalid-next-size-fast">this</a>), but I still can't figure this out. On a few test cases, I'm getting the error and can't make sense of it. The error I'm getting is:</p>
  
  <pre><code>free(): invalid next size (fast)
  </code></pre>
  
  <p>My code uses the Robot Coin Collection algorithm. My implementation of it is below:</p>
  
  <pre><code>int collectTens( vector&lt; vector&lt;int&gt; &gt; grid ) {
  
  vector&lt;vector&lt;int&gt;&gt; result(grid.size(), vector&lt;int&gt;(grid.size(), 0));
  int rows = grid.size();
  int cols = grid[0].size();
  
  for (int i = 0; i &lt; rows; i++) {
      for (int j = 0; j &lt; cols; j++) {
          if (grid[i][j] % 10 != 0) {
              grid[i][j] = 0;
          }
      }
  }
  
  result[0][0] = grid[0][0];
  
  for (int k = 1; k &lt; cols; k++) {
      result[0][k] = result[0][k - 1] + grid[0][k];
  }
  
  for (int i = 1; i &lt; rows; i++) {
      result[i][0] = result[i - 1][0] + grid[i][0];
      for (int j = 1; j &lt; cols; j++) {
          result[i][j] = max(result[i - 1][j], result[i][j - 1]) + grid[i][j];
      }
  }
  
  cerr &lt;&lt; result[rows - 1][cols - 1] &lt;&lt; endl;
  
  return result[rows - 1][cols - 1];
  }
  </code></pre>
  
  <p>In the test cases that the error occurs, the result can be outputted using stderr but will not be able to return anything due to the error. Which is the part that doesn't really make sense to me, because it is the same position in the 2D vector.</p>