1

I am having trouble understanding how to write the destructor in the second class of the program below:

class First
{
    // Assume this is a virtual class
};

class Second
{
    private:
        int index;
        First **arr;

    public:
        Second(int size)
        {
            index = 0;
            arr = new First*[size]; // Please bear with my use of new
        }

        ~Second() {}

        void add(First *f)
        {
            arr[index++] = f;
        }
};

In all of the similar questions I found, each element of the array is assigned a value dynamically, using new as such: arr[i] = new First(); . However, here the elements are assigned the value of a pointer to an object that is a parameter of the function. So, should the destructor delete every element one by one and then delete the array, or is ti enough to delete the array?

~Second()
{
    for(int i = 0; i < index; ++i) delete[] arr[i]; // Is this necessary?
    delete[] arr;
}
15
  • 4
    arr was new[]-ed, so it should be detele[]-ed. Was arr[i] new[]-ed? Commented Apr 2, 2018 at 19:52
  • 4
    Replace First **arr with std::vector<First*> or, if Second owns the Firsts, with std::vector<std::unique_ptr<First>>. Commented Apr 2, 2018 at 20:02
  • 1
    It depends on how the pointers f were created, new, new[], malloc, or &variable? And also on whether the code calling add will retain ownership and delete the objects later. "Painting yourself into a corner" is a phrase that comes to mind. Commented Apr 2, 2018 at 20:02
  • 2
    @DimK - It would have to be a matching call to delete, delete[], free, or nothing. But if you don't know what the caller does, you cannot write it. :-) Commented Apr 2, 2018 at 20:11
  • 2
    No I wouldn't know that there's no other person. You have said you are not sure how that pointer is allocated. Why are you unsure if you are the one who is doing it? If you are writing the entire program you need to decide which pointers are owning objects they point at and which ones are not. There must be exactly one owning pointer for every object at any given moment in time (though different pointers may own it at different moments, but you better avoid that situation). You assign that role. arr owns the array of pointers. What owns objects pointed to by array elements? Commented Apr 2, 2018 at 21:31

2 Answers 2

1

In all of the similar questions I found, each element of the array is assigned a value dynamically, using new as such: arr[i] = new First(); . However, here the elements are assigned the value of a pointer to an object that is a parameter of the function. So, should the destructor delete every element one by one and then delete the array, or is it enough to delete the array?

That, we cannot answer. Does Second take ownership of the objects passed to .add(), and if so how were they allocated?

  1. If it does not take ownership, just deleting the array is enough, and that array should be managed by a std::unique_ptr doing so for you.

  2. If it does take ownership, that argument to .add() should be the smart-pointer with the right ownership-semantics and deleter. Your array should then be an array of those smart-pointers, managed by a std::unique_ptr.

In either case, if you properly use smart-pointers, the default-dtor is fine.

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

1 Comment

Understood. Thanks for replying. Is the use of smart pointers the only proper way to handle this?
1

You'd better keep NULLs in the array after allocation in the constructor first.

    int arr_size; // you need to define this for the reference in destructor

    Second(int size)
    {
        arr_size = size;
        arr = new First*[size]; // Please bear with my use of new
        for (int i = 0; i < size; i++)
            arr[i] = NULL;
    }

Then, in the destructor, delete the element only if it's not NULL as below.

    ~Second()
    {
        for(int i = 0; i < arr_size; i++)
            if (arr[i]) 
                delete arr[i];
        delete[] arr;
    }

2 Comments

Did you miss index, and how it is used?
@Deduplicator, no I left the usage of index for add() to count the real number of elements in the array as it is.

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.