0

I have a Aborted(Core dumped) error during the execution of the following program.

#include<iostream>
using namespace std;
class Array
{
      int *m_ptr;
      int m_size;
      public:
             Array(int sz)
             {
                       cout<<"constructor\n";
                       m_size = sz;
                       m_ptr = new int[sz];
             }
             ~Array()
             {
                     cout<<"Delete\n";
                     delete[] m_ptr;
             }
             int& operator[] (int j)
             {
                  cout<<"Operation []\n";
                  return m_ptr[j];
             }
             int& operator[] (int j) const
             {
                  cout<<"Operation []\n";
                  return m_ptr[j];
             }
             void copy(const Array& ar)
             {
                  m_size = ar.m_size;
                  m_ptr = new int[m_size];
                  int *ptr = ar.m_ptr;
                  int j;
                  for(j = 0;j < m_size; j++)
                  m_ptr[j] = ptr[j];
             }
             Array(const Array& ar)
             {
                                copy(ar);
             }
             /*Array& operator= (const Array& ar) //Why this function prevents us from Aborted(Core Dumped)
             {
                    delete m_ptr;
                    copy(ar);
                    return *this;
             }*/
             void print()
             {
                  int i;
                  for(i = 0;i < m_size;i++)
                  cout<<m_ptr[i]<<"  ";
                  cout<<endl;
             }
};
int main()
{
    Array a1(10);
    Array a2(5);
    int i;
    for(i = 0;i < 10;i++)
    {
          a1[i] = 1;
          if(i < 5) a2[i] = 2;
    }
    a1.print();
    a2.print();
    a1 = a2;//If I do not copy a2 in a1, the  Aborted(Core dumped) error disappers
    return 0;
}

Clearly, it is a problem related to memory allocation. The author of the book I am reading has made the function Array& operator= (const Array& ar) but I do not understand how does this function resolve the problem. I learned that I should use a destructor every time I allocate a new object with new. However, in this example I have a destructor and still, if I do not add the function in question, I get Aborted(Core dumped). Can you please explain why do I get this error message and how does the function resolve the issue?

George
  • 193
  • 3
  • 13
  • The commented out code is wrong: it should be `delete[] m_ptr` and the delete should be done after copying. The problem you are seeing is probably a double delete. – Dietmar Kühl Nov 04 '15 at 20:35
  • Also, learning about this memory management and why this happens is good, but for your own code use `std::vector`. It takes care of so much stuff for you, like exception safety in your code for example. – SirGuy Nov 04 '15 at 20:36
  • See [What's the Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – R Sahu Nov 04 '15 at 20:38

0 Answers0