title: Using new and delete on object arrays C++
body: <p><a href="https://stackoverflow.com/questions/677653/does-delete-call-the-destructor">Does delete call the destructor?</a>
  I have modified the code on the first answer a little bit, but i can't catch my wrong. It is being compiled with 0 errors but program crashes when i run it, also devC++ debug mode gives me SIGTRAP signal. I'd really appreciate a second hand here.</p>
  
  <p>So the idea is;
  reform() will delete the old array (if necessary, the second parameter is optional and i'm only using it in constructors because at that moment the pointer is actually null (?)). And then it will allocate it again with new, using the input parameter size. I really have no idea which part might be wrong. Thanks for all answers.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class A
  {
      private:
          int val;
      public:
          A();
          int GetVal();
          void SetVal(int Val);
  };
  
  A::A()
  {
      this-&gt;val = 0;
  }
  
  int A::GetVal()
  {
      return this-&gt;val;
  }
  
  void A::SetVal(int Val)
  {
      this-&gt;val = Val;
  }
  
  
  #define DEF_A_SIZE 1
  
  class B
  {
      private:
          A *a_s;
          int aSize;
          int aCount;
          void reform(const int size, int dlt = 1);
  public:
      B();
      ~B();
      void SetSize(const int newSize);
      int GetSize();
      void AddA(A a);
      A GetA(int index);
  };
  
  void B::reform(const int size, int dlt)
  {
      if(dlt)
      {
          delete[] (this-&gt;a_s);
      }
      this-&gt;a_s = new A[size];
      this-&gt;aCount = 0;
      this-&gt;aSize = size;
  }
  
  B::B()
  {
      reform(DEF_A_SIZE, 0);
  }
  
  B::~B()
  {
      delete [] (this-&gt;a_s);
  }
  
  void B::SetSize(const int newSize)
  {
      reform(newSize);
  }
  
  int B::GetSize()
  {
      return this-&gt;aSize;
  }
  
  void B::AddA(A a)
  {
      if (aCount &lt; aSize)
      {
          this-&gt;a_s[aCount].SetVal(a.GetVal());
          aCount++;
      }
  }
  
  A B::GetA(int index)
  {
      if (index &lt; aCount)
      {
          return this-&gt;a_s[index];
      }
  }
  
  #define B_NUM 1
  
  class C
  {
      private:
          B *b_s;
          int bc;
          int bs;
          void reform(const int size, int dlt = 1);
      public:
          C();
          ~C();
          void addb(B b);
          B getB(int index);
  };
  
  void C::reform(const int size, int dlt)
  {
      if (dlt)
      {
          delete [] (this-&gt;b_s);
      }
      bc = 0;
      bs = size;
      b_s = new B[size];
  }
  
  void C::addb(B b)
  {
      if (bc &lt; bs)
      {
          b_s[bc].SetSize(b.GetSize());
          for (int i = 0; i &lt; b.GetSize(); i++)
          {
              b_s[bc].AddA(b.GetA(i));
          }
          bc++;
      }
  }
  
  B C::getB(int index)
  {
      if (index &lt; bc)
      {
          return b_s[index];
      }
  }
  
  C::C()
  {
      reform(B_NUM, 0);
  }
  
  C::~C()
  {
      delete [] (this-&gt;b_s);
  }
  
  int main()
  {
      C *cptr = new C();
  
      B b = B();
      b.SetSize(2);
  
      A a = A();
      a.SetVal(10);
      b.AddA(a);
  
      A a2 = A();
      a2.SetVal(20);
      b.AddA(a2);
  
      cptr-&gt;addb(b);
  
      cout &lt;&lt; cptr-&gt;getB(0).GetA(0).GetVal() &lt;&lt; endl;
      cout &lt;&lt; cptr-&gt;getB(0).GetA(1).GetVal() &lt;&lt; endl;
  
      delete cptr;
  
      cin.get();
  
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <h3>Solved</h3>
  
  <p>Turns out my problem was my Add or Get functions. I changed them this way;
  Getters will return references to the respective objects (e.g B &amp;getB(int index);)
  And Adder's will take pointers as parameters, not objects ( or values ) (e.g void addb(B *b);) </p>