Frage

Let's say I have the array:

Geometry* shapes[MAX_SIZE];

And then I fill the array like that:

for (int i = 0; i < MAX_SIZE; ++i)
{
    shapes[i] = new Geometry;
}

Will delete [] shapes; do the job or I have to loop through the array and delete one by one :

for (int i = 0; i < MAX_SIZE; ++i)
{
    delete shapes[i];
}

I think I have to loop and delete individually each pointer, because delete [] calls the destructors of the objects in the array which doesn't mean that the memory will be freed. But I need a confirmation from someone more experienced.

War es hilfreich?

Lösung

You have an plain array of pointers. Each pointer is made to point to an individual dynamically allocated Geometry. So you need to call delete in each of those pointers. delete [] is for dynamically allocated arrays.

In real life you would use a types that manage the resources automatically, to avoid having to manually delete the pointers. Options include arrays of std::unique_ptr<Geometry>, or containers designed to manage dynamically allocated objects, such as those available in the boost::pointer_container library.

Andere Tipps

Loop through the array and delete the items one by one.

The array itself seems to be statically allocated, so you don't need to delete it.

In fact, if you do try to delete[] shapes, then you will probably get a runtime exception.

as juanchopanza said...

take arrays of managed pointers or boost::pointer_container. tests i did for myself, showed that pointer_container are faster

 time elapsed for 1000 adds: 
      0 ms     364 us when using std::vector<boost::shared_ptr<Foo> >
      0 ms     205 us when using boost::ptr_vector<Foo>
 time elapsed for 10000 adds: 
      3 ms    3979 us when using std::vector<boost::shared_ptr<Foo> >
      2 ms    2195 us when using boost::ptr_vector<Foo>
 time elapsed for 100000 adds: 
     39 ms   39514 us when using std::vector<boost::shared_ptr<Foo> >
     21 ms   21365 us when using boost::ptr_vector<Foo>
 time elapsed for 1000000 adds: 
    363 ms  363687 us when using std::vector<boost::shared_ptr<Foo> >
    206 ms  206395 us when using boost::ptr_vector<Foo>
 time elapsed for 10000000 adds: 
   3887 ms 3887172 us when using std::vector<boost::shared_ptr<Foo> >
   2084 ms 2084816 us when using boost::ptr_vector<Foo>

at least for the vector variant. it think it is worth a try for the arrays as well

If you can use C++11 look at new memory library.

You can change standard C++ pointers to one of:

Or sth else from there. Then you don't need to remember freeing objects dynamically declared

Four answers and no one has mentioned

std::unique_ptr<Geometry[]> shapes(new Geometry[MAX_SIZE]);

The array specialization std::unique_ptr<T[]> is what you want.

EDIT: Oops I misread the question and just realized you need an array of pointers not just an array. I'll leave this answer here in case someone can use this.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top