title: C++ delete [] object causes crash
body: <p>I made a dynamic array of objects. When I call delete[] , program crashes and stops responding. But it does strange behavior: calls all destructors and crashes. look at this code and output. All the memory for all arrays is allocated.</p>
  
  <pre><code>//main file
  #include "myobject.h"
  
  int size;
  myObject *foo;
  
  //some operations to estimate size
  
  foo = new myObject[size];
  
  //some operations with myObject
  
  std::cout&lt;&lt;"size: "&lt;&lt;size&lt;&lt;"\n";
  std::cout&lt;&lt;"Deleting object\n";
  
  size=0;
  delete [] foo;
  </code></pre>
  
  <p>Next file:</p>
  
  <pre><code>//myobject.h
  
  class myObject
  {
      public:
      int number;
  
      Object1 ob1[64]
      Object2 *ob2;
  
      myObject(){ };
      ~myObject()
      {
          std::cout&lt;&lt;"delete "&lt;&lt;number&lt;&lt;"\n";
  
          delete [] ob1;
          delete [] ob2;
      };
  }
  </code></pre>
  
  <p>And the output:</p>
  
  <pre><code>size: 11
  
  Deleting object
  
  delete 10
  
  delete 9
  
  delete 8
  
  delete 7
  
  delete 6
  
  delete 5
  
  delete 4
  
  delete 3
  
  delete 2
  
  delete 1
  
  delete 0
  </code></pre>
  
  <p>And then it crashes and stops responding. </p>