1

Say I want to declare an array of pointers to a specific object like

Object *objects = new Object[size];

and then add some new objects to my dynamically allocated array

for (int i = 0; i < size; i++){

    Object* obj = new obj;
    objects[i] = *obj;

}

is it enough to deallocate all memory by just calling delete[] on the array, or do I have to loop through the array first and call delete on each object? Or is this stupid to do in practice?

1
  • You're not declaring an array of pointers. Do you really want to declare one? Commented May 31, 2015 at 21:38

3 Answers 3

5

You always have to delete everything you new in some way. That means, you would need to delete obj in every iteration to avoid leaks. Note that you never really store the Object obj points to, but a copy of it.

The way you do it right know is quite unusual and not very handy anyway: The loop you showed does nothing useful since the new Objects[size] already default constructed your Objects. In particular, it does not add any elements, they are already there. You could just leave that out. If you want to change the content of your array, e.g. do more initialization, your loop would usually look more like this

for (int i = 0; i < size; i++){
    objects[i] = newValue;    
}

Remember, the Objects are already there after new Objects[size]!

In practice, you would be much better of with a

std::vector<Object> objects(size);

(or maybe, if the above does not fit your usecase, a

std::vector<std::unique_ptr<Object>> objects(size);

, but I feel like that is unlikely to be the case). For a good overview of what a std::vector can do and how to use it, read the documentation. Probably most importantly for you: You can index it just like an array.

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

Comments

3

First, you have to follow the deallocation is reverse order of allocation.

So number of times new that many times delete , similarly number of times new [] that many times delete []


// Assuming objects as array of pointers [ Object **objects ]
for (int i = 0; i < size; i++) 
{
   delete objects[i] ;
}

delete [] objects;

And your correct allocation should go as:

Object **objects= new Object*[size];
for (int i = 0; i < size; i++) { 
    objects[i] = new Object;
}

Also , you should use smart pointers std::unique_ptr for hassle free memory management

std::vector< std::unique_ptr<Object> > objects ;

Comments

1
  1. You are storing an array of Objects and not pointers to Object. This means that they won't be deleted when you delete the array and the memory will leak.
  2. If you want to store pointers to Objects in your array then you need to declare it as Object **objects = new Object *[size]; and then you will have to delete all the pointers in the array.

So the better code will look like:

//Allocation:
Object **objects = new Object *[size];
for (int i = 0; i < size; i++){
    objects[i] = new Object;
}

//Deletion
for (int i = 0; i < size; i++){
    delete objects[i];
}
delete [] objects;

An even better solution would be to use vectors and smart pointers and then you don't need the deletion code at all.

//Allocation:
vector<unique_ptr<Object>> objects;
for (int i = 0; i < size; i++){
    objects.push_back(make_unique(new Object));
}

//Deletion
//Hey where did all the code go.

Comments

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.