title: Memory overwrite in my own Vector class
body: <p><br/>
  For my Algorithm course project we can't use STL stuff like <code>std::vector</code> and so I'm trying to implement my own version of it (with templates).<br/></p>
  
  <p>It seems it works but when I declare a <code>Vector&lt; Vector&lt; int > >
  </code> the <code>.push()</code> method starts to overwrite memory.</p>
  
  <p>More specifically, with this code:</p>
  
  <pre><code>Vector&lt;Vector&lt;int&gt;&gt; v(3);
  
  cout &lt;&lt; v[0].push(0) &lt;&lt; "\n";
  cout &lt;&lt; v[0].push(55) &lt;&lt; "\n";
  cout &lt;&lt; v[0].push(4) &lt;&lt; "\n";
  cout &lt;&lt; v[1].push(12) &lt;&lt; "\n";
  cout &lt;&lt; v[1].push(3) &lt;&lt; "\n";
  cout &lt;&lt; v[2].push(1) &lt;&lt; "\n";
  </code></pre>
  
  <p>The output is this (<code>.push()</code> returns the address of where the element is inserted):</p>
  
  <pre><code>0x561328b0bc20
  0x561328b0bc24
  0x561328b0bc28
  0x561328b0bc20
  0x561328b0bc24
  0x561328b0bc20
  </code></pre>
  
  <p>Any suggestion of why this happens?</p>
  
  <p>Here is the code for my <code>Vector</code> class:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;bits/stdc++.h&gt;
  using namespace std;
  
  template&lt;class T&gt;
  class Vector {
  private:
      size_t  _size;
      size_t  _capacity;
      char*   _buffer; //char* for performance
  
      void _realloc(size_t);
  
  public:
      Vector(size_t s=0, T def=T());
  
      T* push(T);
      T&amp; operator[](int);
      size_t size() { return _size; }
  
  };
  
  template&lt;class T&gt;
  void Vector&lt;T&gt;:: _realloc(size_t ncap)
  {
      _capacity = ncap;
      char* nbuf = _buffer;
      _buffer = new char[_capacity];
      for(size_t i=0; i&lt;_size * sizeof(T); ++i)
          _buffer[i] = nbuf[i];
      delete[] nbuf;
  }
  
  /*
   * s -&gt; size
   * def -&gt; default value
   */
  template&lt;class T&gt;
  Vector&lt;T&gt;:: Vector(size_t s, T def) : _size(s)
  {
      _capacity = 32;
      while(_capacity &lt; _size)
          _capacity *= 2;
  
      _buffer = new char[_capacity * sizeof(T)];
      for(size_t i=0; i&lt;_size; ++i)
          ((T*)_buffer)[i] = def;
  }
  
  /*
   * check capacity, reallocs if necessary and push the element
   * then return the addres (used only for debug)
   */
  template&lt;class T&gt;
  T* Vector&lt;T&gt;:: push(T ele)
  {
      if(_capacity == _size)
          _realloc(2 * _capacity);
      ((T*)_buffer)[_size++] = ele;
  
      return &amp;((T*)_buffer)[_size-1];
  }
  
  template&lt;class T&gt;
  T&amp; Vector&lt;T&gt;:: operator[](int i)
  {
      if(i&lt;0 or i&gt;=(int)_size) {
          cerr &lt;&lt; "Out of bounds!\n";
          abort();
      }else
          return ((T*)_buffer)[i];
  }
  
  template&lt;class T&gt;
  ostream&amp; operator&lt;&lt;(ostream&amp; out, Vector&lt;T&gt;&amp; v)
  {
      out &lt;&lt; "{";
      if(v.size() &gt; 0) {
          out &lt;&lt; v[0];
          for(size_t i=1; i&lt;v.size(); ++i)
              out &lt;&lt; ", " &lt;&lt; v[i];
      }
      out &lt;&lt; "}";
      return out;
  }
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>PS: I know it's not a good use of C++ :P</p>