title: C++ heap corrupt
body: <pre><code>int main()
  {
      int a = 3;
      int arr[] = { 5, 3, 6 };
      Vect v(a, arr);
      v.add(1);
      v.add(2);
      v.add(3);
      v.add(4);
      return 0;
  }
  </code></pre>
  
  <p><code>Vect</code> class:</p>
  
  <pre><code>int n;
  int* ptr = new int[n];
  </code></pre>
  
  <p>Constructor:</p>
  
  <pre><code>Vect(int nInit, int arr[])
  {
      n = nInit;
      for (int i = 0; i &lt; n; i++)
      {
          ptr[i] = tab[i];
      }
  }
  </code></pre>
  
  <p>add method in <code>Vect</code> class:</p>
  
  <pre><code> void add(int value)
   {
      n += 1;
      ptr[n-1] = value;
   }
  </code></pre>
  
  <p>I'm getting the error <code>Program.exe has triggered a breakpoint.</code> at 
  <code>msvcr120d.dll!_heap_alloc_base(unsigned int size) Line 58</code>.
  I've also tried using <code>memcpy</code>.</p>
  
  <p>The interesting thing is that when I only call <code>add</code> once or twice, or when sending only one <code>int</code> in the <code>arr</code> array, everything is okay.</p>
  
  <p>What could be going wrong here?</p>