0

I got stuck with deleting an dynamically allocated array of int. I've got a destructor, where I'm trying to use a loop for to delete all elements of array and finally delete it. I have code on http://rextester.com/OTPPRQ8349 Thanks!

class MyClass
{
   public:
   int _a;
   int* c;
   int fRozmiar;
   static int fIlosc;
   MyClass() //default constructor
   {
       _a=0;
       c = new int [9];
       for(int i = 0; i<=9; i++)
       {
           c[i] = 1;
       }
       fIlosc++;
   }
   MyClass(int a1, int c1) // parametrized constructor
   {
       _a=a1;
       c = new int [c1];
       for(int i = 0; i<=c1; i++)
       {
           c[i] = rand();
       }
       fIlosc++;
   }
   MyClass(const MyClass &p2) // copy constructor
   {
    _a =p2._a;
    c = p2.c;
    fRozmiar = p2.fRozmiar;
    fIlosc = fIlosc;
    fIlosc++;
   }
    ~MyClass(); // destructor

    static int getCount() {
         return fIlosc;
      }
};

//Initialize static member of class
int MyClass::fIlosc = 0;
MyClass::~MyClass()
{
        for(int i = 0; i<sizeof(c); ++i)
        {
            delete[] c[i];
        }
        delete[] c;
       fIlosc--;
}
int main()
{
}
6
  • This works rextester.com/CMN81398 Commented Nov 18, 2017 at 0:17
  • Deletion must be symmetric to allocation! You allocated the array in a once, then deallocated it in a once. Commented Nov 18, 2017 at 0:24
  • 1
    Unrelated: sizeof(c) isn't going to give you the size of the buffer pointed at by c. It will give you the size of the pointer, the size of an address, in bytes. Not very useful most of the time. Commented Nov 18, 2017 at 0:28
  • 1
    Unrelated: in the copy constructor c = p2.c; is a fatal error. You now have two objects pointing at the same allocation. When the destructor runs for one of these objects the memory will be gone for the other object. If the program doesn't crash over the other object trying to use released memory, something horrible (probably a crash) will happen when the other object is deleted and tries to delete memory that has already been deleted. Commented Nov 18, 2017 at 0:32
  • Unrelated: fIlosc = fIlosc; assigning a variable to itself. Since this variable is static, it will be the same for both objects so there's no point to doing this. Commented Nov 18, 2017 at 0:34

2 Answers 2

4

Remove the for-loop, but keep the delete[] c after it.

Each int doesn't need to be deleted because they're not dynamically allocated. If you needed to delete them, then the for-loop wouldn't work becuase: sizeof(c) is not the size of the array, and delete[] should have been delete instead.

Sign up to request clarification or add additional context in comments.

3 Comments

unfortunately delete[] c does not work. You can check it, I added link to online c++ compiler
The delete in the for-loop doesn't work, remove that one. delete[] c should work though.
@tylkonachwile That line isn’t generating an error; the one inside the for loop is. Here’s the way to understand it yourself: the new expression gives you a pointer; this pointer and only this pointer should be passed to delete. The delete should have [] if, and only if, the corresponding new did. You don’t want to delete every element of an array, unless it’s an array of pointers and each of those pointers corresponds to something that was also newd.
3

There are several problems in the code.

First, that loop in the destructor must go. If you didn’t new it, don’t delete it.

Second, a loop through an array of N elements should be for (int i = 0; i < N; ++i). Note that the test is i < N, not i <= N. The loops as currently written go off the end of the array. That’s not good.

Third, the copy constructor copies the pointer. When the first object goes out of scope its destructor deletes the array; when the copy goes out of scope its destructor also deletes the array. Again, not good. The copy constructor has to make a copy of the array. In order to do that the class needs to also store the number of elements the array.

1 Comment

wow, an amazing approch, explained more than necessary, now everything is clear, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.