title: How can I locate error: *** Error in `./a.out': free(): invalid next size (fast): 0x09767150 ***
body: <p>My main.cpp and grid.h compile successfully.  The problem occurs when I use the function fill after resizing the grid.  This produces an error after running the program:</p>
  
  <blockquote>
    <p><strong>* Error in `./a.out': free(): invalid next size (fast): 0x0871d150 *</strong></p>
  </blockquote>
  
  <p>main.cpp</p>
  
  <pre><code>#include "grid.h"
  #include &lt;cstdlib&gt;
  
  #if 1
      #define log(x) std::cout &lt;&lt; x &lt;&lt; std::endl;
  #else
      #define log(x) 
  #endif  
  
  #ifdef _GLIBCXX_CSTDLIB
      #define clear system("clear");
      #define reset system("reset");
  #else
      #define clear
      #define reset
  #endif
  
  std::pair&lt;const int, const int&gt; LARGE  = std::make_pair(8,8);
  std::pair&lt;const int, const int&gt; MEDIUM = std::make_pair(6,6);
  std::pair&lt;const int, const int&gt; SMALL  = std::make_pair(4,4); 
  
  
  int main(){
      clear
      grid&lt;char&gt; a(LARGE,'#');
      grid&lt;int&gt;  b(4,5,   9 );
      a.resize(4,8);
      b.resize(MEDIUM);
      b.fill(8);
      log(a);
      log(b);
      return 0;
  }
  </code></pre>
  
  <p>grid.h</p>
  
  <pre><code>#ifndef GRID_H
  #define GRID_H
  
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  template&lt;typename type&gt;
  class grid{
      public://Functions
          grid(std::pair&lt;const type, const type&gt; dimension, type filler = 0) : rows(dimension.first), cols(dimension.second){
                  matrix.assign(rows, std::vector&lt;type&gt;(cols, filler));
          };
          grid(const int _rows, const int _cols, type filler = 0) : rows(_rows), cols(_cols){
                  matrix.assign(rows, std::vector&lt;type&gt;(cols, filler));
          };
          void resize(std::pair&lt;const type, const type&gt; dimension){
              rows = dimension.first, cols = dimension.second;
              matrix.resize(rows, std::vector&lt;type&gt;(cols));
          };
          void resize(const int _rows, const int _cols){
              rows = _rows, cols = _cols;
              matrix.resize(rows, std::vector&lt;type&gt;(cols));
          };
          void fill(type filler){
              for(int r = 0; r &lt; rows; r++){
                  for(int c = 0; c &lt; cols; c++){
                      matrix[r][c] = filler;
                  }
              }
          };
      public://Operators
          friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, grid&amp; g){
              for(int r = 0; r &lt; g.rows; r++){
                  for(int c = 0; c &lt; g.cols; c++){
                      out &lt;&lt; g.matrix[r][c];
                  }out &lt;&lt; std::endl;
              }return out;
          };
          //Variables
          std::vector&lt;std::vector&lt;type&gt;&gt; matrix;
          int  rows;
          int  cols;
  };
  
  #endif//GRID_H
  </code></pre>
  
  <p>Console Output</p>
  
  <pre><code>dylan@Aspire-one:~$ ./a.out
  ########
  ########
  ########
  ########
  
  888888
  888888
  888888
  888888
  888888
  888888
  
  *** Error in `./a.out': free(): invalid next size (fast): 0x09767150 ***
  Aborted (core dumped)
  dylan@Aspire-one:~$ 
  </code></pre>