title: Implementation of Vector in C++
body: <p>I recently wrote an implementation of STL Vector as a programming exercise. The program compiles but I receive a strange error saying: </p>
  
  <pre><code>terminate called after throwing an instance of 'std::bad_alloc'
    what():  std::bad_alloc
  </code></pre>
  
  <p>I've never come up with this error before and am not sure what exactly should be changed within my implementation to make it function correctly.</p>
  
  <p>Can someone take a look through my code and see if anything jumps out at them as wrong in this specific case? Sorry I can't be more specific, I'm not sure where to look myself, thanks in advance.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;cassert&gt;
  #include &lt;algorithm&gt;
  
  using namespace std;
  
  template &lt;class T&gt;
  class Vector
  {
  public:
  
     typedef T * iterator;
  
     Vector();
     Vector(unsigned int size);
     Vector(unsigned int size, const T &amp; initial);
     Vector(const Vector&lt;T&gt; &amp; v);      
     ~Vector();
  
     unsigned int capacity() const;
     unsigned int size() const;
     bool empty() const;
     iterator begin();
     iterator end();
     T &amp; front();
     T &amp; back();
     void push_back(const T &amp; value); 
     void pop_back();  
  
     void reserve(unsigned int capacity);   
     void resize(unsigned int size);   
  
     T &amp; operator[](unsigned int index);  
     Vector&lt;T&gt; &amp; operator=(const Vector&lt;T&gt; &amp;);
  
  private:
     unsigned int my_size;
     unsigned int my_capacity;
     T * buffer;
  };
  
  // Your code goes here ...
  template&lt;class T&gt;
  Vector&lt;T&gt;::Vector()
  {
      my_capacity = 0;
      my_size = 0;
      buffer = 0;
  }
  
  template&lt;class T&gt;
  Vector&lt;T&gt;::Vector(const Vector&lt;T&gt; &amp; v)
  {
      my_size = v.my_size;
      my_capacity = v.my_capacity;
      buffer = new T[my_size];  
      for (int i = 0; i &lt; my_size; i++)
          buffer[i] = v.buffer[i];  
  }
  
  template&lt;class T&gt;
  Vector&lt;T&gt;::Vector(unsigned int size)
  {
      my_capacity = size;
      my_size = size;
      buffer = new T[size];
  }
  
  template&lt;class T&gt;
  Vector&lt;T&gt;::Vector(unsigned int size, const T &amp; initial)
  {
      my_size-size;
      my_capacity = size;
      buffer = new T [size];
      for (int i = 0; i &lt; size; i++)
          buffer[i] = initial;
          T();
  }
  
  template&lt;class T&gt;
  Vector&lt;T&gt; &amp; Vector&lt;T&gt;::operator = (const Vector&lt;T&gt; &amp; v)
  {
      delete[ ] buffer;
      my_size = v.my_size;
      my_capacity = v.my_capacity;
      buffer = new T [my_size];
      for (int i = 0; i &lt; my_size; i++)
          buffer[i] = v.buffer[i];
      return *this;
  }
  
  template&lt;class T&gt;
  typename Vector&lt;T&gt;::iterator Vector&lt;T&gt;::begin()
  {
      return buffer;
  }
  
  template&lt;class T&gt;
  typename Vector&lt;T&gt;::iterator Vector&lt;T&gt;::end()
  {
      return buffer + size();
  }
  
  template&lt;class T&gt;
  T&amp; Vector&lt;T&gt;::Vector&lt;T&gt;::front()
  {
      return buffer[0];
  }
  
  template&lt;class T&gt;
  T&amp; Vector&lt;T&gt;::Vector&lt;T&gt;::back()
  {
      return buffer[size - 1];
  }
  
  template&lt;class T&gt;
  void Vector&lt;T&gt;::push_back(const T &amp; v)
  {
      if (my_size &gt;= my_capacity)
      reserve(my_capacity +5);
      buffer [my_size++] = v;
  }
  
  template&lt;class T&gt;
  void Vector&lt;T&gt;::pop_back()
  {
      my_size--;
  }
  
  template&lt;class T&gt;
  void Vector&lt;T&gt;::reserve(unsigned int capacity)
  {
      if(buffer == 0)
      {
          my_size = 0;
          my_capacity = 0;
      }    
      T * buffer = new T [capacity];
      assert(buffer);
      copy (buffer, buffer + my_size, buffer);
      my_capacity = capacity;
      delete[] buffer;
      buffer = buffer;
  
  }
  
  template&lt;class T&gt;
  unsigned int Vector&lt;T&gt;::size()const//
  {
      return my_size;
  }
  
  template&lt;class T&gt;
  void Vector&lt;T&gt;::resize(unsigned int size)
  {
      reserve(size);
      size = size;
  }
  
  template&lt;class T&gt;
  T&amp; Vector&lt;T&gt;::operator[](unsigned int index)
  {
      return buffer[index];
  }  
  
  template&lt;class T&gt;
  unsigned int Vector&lt;T&gt;::capacity()const
  {
      return my_capacity;
  }
  
  template&lt;class T&gt;
  Vector&lt;T&gt;::~Vector()
  {
      delete[]buffer;
  }
  
  
  int main()
  {  
  
     Vector&lt;int&gt; v;
  
     v.reserve(2);
     assert(v.capacity() == 2);
  
     Vector&lt;string&gt; v1(2);
     assert(v1.capacity() == 2);
     assert(v1.size() == 2);
     assert(v1[0] == "");
     assert(v1[1] == "");
  
     v1[0] = "hi";
     assert(v1[0] == "hi");
  
     Vector&lt;int&gt; v2(2, 7);
     assert(v2[1] == 7);
  
     Vector&lt;int&gt; v10(v2);
     assert(v10[1] == 7);
  
     Vector&lt;string&gt; v3(2, "hello");
     assert(v3.size() == 2);
     assert(v3.capacity() == 2);
     assert(v3[0] == "hello");
     assert(v3[1] == "hello");
  
     v3.resize(1);
     assert(v3.size() == 1);
     assert(v3[0] == "hello");
  
     Vector&lt;string&gt; v4 = v3;
     assert(v4.size() == 1);
     assert(v4[0] == v3[0]);
     v3[0] = "test";
     assert(v4[0] != v3[0]);  
     assert(v4[0] == "hello");
  
     v3.pop_back();
     assert(v3.size() == 0);
  
     Vector&lt;int&gt; v5(7, 9);
     Vector&lt;int&gt;::iterator it = v5.begin();
     while (it != v5.end())
     {
        assert(*it == 9);
        ++it;
     }
  
     Vector&lt;int&gt; v6;
     v6.push_back(100);
     assert(v6.size() == 1);
     assert(v6[0] == 100);
     v6.push_back(101);
     assert(v6.size() == 2);
     assert(v6[0] == 100);
     v6.push_back(101);
  
     cout &lt;&lt; "SUCCESS\n";
  }
  </code></pre>