1

I am creating a program that uses an array of objects declared with

Element * elements = new Element[number];

where an element is a class that has/needs a its own destructor.

when I go to delete this would I use just use array delete, and have the program worry about calling the destructor:

delete [] elements;

or do I call the destructor for each item explicitly with keyword delete:

for(int ii = 0; ii< ArraySize; ii++)
    delete elements[ii];
delete [] elements;

Note: I understand that I could probably use something like boost::ptr_vector, but I wanted similar to hashTable functionality (so the for loop would need additional information, but that is outside of the direct scope of this question) which is why I am using a traditional array. I would still like to know which behavior is needed to avoid memory leaks.

1
  • You're not storing pointers in your array, so delete[] should do the trick. Commented May 3, 2012 at 4:58

2 Answers 2

3

The first one. You should

delete [] elements;

The second one is incorrect and should give you errors if you try to compile it.

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

2 Comments

though the object in the array will need the destructor called, and so basically just deleting the array will call the destructor for each object
Yes, delete [] elements will free the memory for the entire array and call the destructor for each object. You delete what you new, and delete [] what you new []. Here, you only did new [], so you only need delete [].
1

Yes, delete [] elements; should be sufficient.

You'd use your second piece of code with something like:

Element **elements;

elements = new Element *[rows];
for (int i=0; i<rows; i++)
    elements[i] = new Element;

This combination rarely makes much sense though. To make at least some sense, each row would itself be a dynamically allocated array as well:

elements = new Element *[rows];
for (int i=0; i<rows; i++)
    elements[i] = new Element[row_len];

In this case, your deletion would look something like:

for (int i=0; i<rows; i++)
    delete [] elements[i];
delete [] elements;

As you're doing it right now, however, none of what you've said really justifies home-rolled dynamic allocation at all. std::vector<Element> elements(number); would work perfectly well.

2 Comments

I am wanting a hash table functionality that actually beats out even std::vector in terms of operations HashTable is approximately O(2) even for large tables assuming less then 75% full, while even the most well implemented vector/contiguous list is O(n), and this array is expected to handle a large number of objects.
@gardian06: But what you're doing right now is creating a contiguous array, just like std::vector would. To implement a hash table, you probably want std::vector<Element *> instead. Of course, for a hash table, your first choice should normally be std::unordered_map<Element>.

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.