title: Adding to dynamic array
body: <p>Disclaimer: Yes, I know about std::vector. I'm doing this for the sake of learning.</p>
  
  <p>I'm working on making a dynamic array class, and I'm trying to get add to work.</p>
  
  <pre><code>template &lt;class T&gt;
  void Array&lt;T&gt;::add(T value)
  {
      T * tmp = new T[mCount];
  
      for (int i = 0; i &lt; mCount; i++)
      {
          tmp[i] = mData[i];
      }
  
      mCount++;
  
      delete[] mData;
      mData = tmp;
  
      mData[mCount - 1] = value;
  }
  </code></pre>
  
  <p>It works... sort of. The function works in adding the element, but then the program crashes when exiting. No errors, no nothing. It just freezes, and I have to close it using (Shift + F5).</p>
  
  <p>So, what's wrong with this?</p>
  
  <p>Here's my whole class. If I didn't include a function it means there's no code in it.</p>
  
  <pre><code>#ifndef ARRAY_H
  #define ARRAY_H
  
  using namespace std;
  
  template &lt;class T&gt;
  class Array
  {
  private:
      T * mData;
      int mCount;
  
  public:
      Array();
      ~Array();
  
      void add(T value);
      void insert(T value, int index);
      bool isEmpty();
      void display();
      bool remove(T value);
      bool removeAt(int index);
      int size();
  
      T &amp; operator[](const int index);
  };
  
  // Constructors / Destructors
  // --------------------------------------------------------
  
  template &lt;class T&gt;
  Array&lt;T&gt;::Array()
  {
      mCount = 0;
      mData = new T[mCount];
      for (int i = 0; i &lt; mCount; i++)
          mData[i] = 0;
  }
  
  template &lt;class T&gt;
  Array&lt;T&gt;::~Array()
  {
      delete[] mData;
  }
  
  // General Operations
  // --------------------------------------------------------
  
  template &lt;class T&gt;
  void Array&lt;T&gt;::add(T value)
  {
      T * tmp = new T[mCount];
  
      for (int i = 0; i &lt; mCount; i++)
      {
          tmp[i] = mData[i];
      }
  
      mCount++;
  
      delete[] mData;
      mData = tmp;
  
      mData[mCount - 1] = value;
  }
  
  template &lt;class T&gt;
  void Array&lt;T&gt;::display()
  {
      if (isEmpty())
      {
          cout 
              &lt;&lt; "The array is empty."
              &lt;&lt; "\n\n";
          return;
      }
  
      cout &lt;&lt; "(";
  
      for (int i = 0; i &lt; mCount; i++)
      {
  
          cout &lt;&lt; mData[i];
  
          if (i &lt; mCount - 1)
              cout &lt;&lt; ", ";
      }
  
      cout &lt;&lt; ")" &lt;&lt; "\n\n";
  }
  
  template &lt;class T&gt;
  bool Array&lt;T&gt;::isEmpty()
  {
      return mCount == 0;
  }
  
  template &lt;class T&gt;
  int Array&lt;T&gt;::size()
  {
      return mCount;
  }
  
  // Operator Overloads
  // --------------------------------------------------------
  
  template &lt;class T&gt;
  T &amp; Array&lt;T&gt;::operator[](const int index)
  {
      return mData[index];
  }
  
  #endif
  </code></pre>
  
  <p>If you need any additional info lemme know and I can post it.</p>