简体   繁体   中英

Deleting an Array of Pointers - Am I doing it right?

I feel a little stupid for making a question about the deletion of pointers but I need to make sure I'm deleting in the correct way as I'm currently going through the debugging process of my program.

Basically I have a few arrays of pointers which are defined in my header file as follows:

AsteroidView *_asteroidView[16];

In a for loop I then initialise them:

for(int i = 0; i < 16; i++)  
{  
      _asteroidView[i] = new AsteroidView();  
} 

Ok, so far so good, everything works fine. When I eventually need to delete these in the destructor I use this code:

for(int i = 0; i < 16; i++)  
{  
        delete _asteroidView[i];  
}

Is this all I need to do? I feel like it is, but I'm worried about getting memory leaks.

Out of interest... Is there much of a difference between an Array of Points to Objects compared with an Array of Objects?

This is correct. However, you may want to consider using Boost.PointerContainer , and save you hassle the hassle of manual resource management:

boost::ptr_vector<AsteroidView> _asteroidView;
for(int i = 0; i < 16; i++)
{
    _asteroidView.push_back(new AsteroidView());
}

You do not have to manage the deletion, the container does that for you. This technique is called RAII , and you should learn about it if you want to have fun using C++ :)

About your edit: There are several difference, but I guess the most important are these:

  1. An array of pointers can contain objects of different types, if these are subclasses of the array type.
  2. An array of objects does not need any deletion, all objects are destroyed when the array is destroyed.

It's absolutely fine.

The rule of thumb is: match each call to new with an appropriate call to delete (and each call to new[] with a call to delete[] )

Is this all I need to do? I feel like it is, but I'm worried about getting memory leaks.

Yes. The program is deallocating resources correctly. No memory leaks :)


If you are comfortable with using std::vector ( infact it is easy ), it does the deallocation process when it goes out of scope. However, the type should be of -

std::vector<AsteroidView>

Given a class:

// this class has hidden data and no methods other than the constructor/destructor
// obviously it's not ready for prime time
class Foo {
    int* bar_[16];

    public:
    Foo()
    {
        for (unsigned int i = 0; i < 16; ++i)
            bar_[i] = new int;
    }

    ~Foo()
    {
        for (unsigned int i= 0; i < 16; ++i)
            delete bar_[i];
    }
};

You won't leak memory if the constructor completes correctly . However, if new fails fails in the constructor ( new throws a std::bad_alloc if you're out of memory), then the destructor is not run , and you will have a memory leak. If that bothers you, you will have to make the constructor exception safe (say, add a try ... catch block around the constructor, use RAII). Personally, I would just use the Boost Pointer Container if the elements in the array must be pointers, and a std::vector if not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM