Sign in to follow this  
Goldstein

Deleting an array of pointers.

Recommended Posts

I hope I'm visualising this right. What I want is an array containing pointers to objects which I may wish to create at a later time, up to a predetermined number. A linked list is one option but I'm hoping that won't be necessary as I know how many objects I want at compile time. Let's imagine I have a class named Obj and would like an array of five Objs, called Obj_Arr. I might do this:
Obj *Obj_Arr = new Obj[5];



I think in this case, Obj_Arr points to a location in memory that is the start of the memory describing five Objs. However, if I then create another Obj and want to 'slot it in' to this array, like this:
Obj foo;
Obj_Arr[0] = foo;



I think that foo's data will be copied into memory at Obj_Arr[0], so the data for foo will now be in memory twice. (Is this true?) The real purpose of my array, though, is to keep track of where my Objs are in memory so I can get rid of them later on. With this in mind, I don't necessarily need to keep an array of Objs, only an array of pointers to Objs. So instead of the above, I've declared this:
Obj **Obj_PtrArr = new Obj*[5];



I might be wrong, but I imagine that Obj_PtrArr points to a location in memory that is the start of five pointers, none of which yet point anywhere in particular (I assume I've only declared five pointers, not five Objs). I think now I really can 'slot in' some Objs, like this:
Obj *foo = new Obj;
Obj_PtrArr[0] = foo;



I imagine this as declaring my new Obj and then copying it's address into my array. In this case, &foo is in memory twice but foo itself, which is presumably bigger, only exists once. (Stop me if I'm wrong). Assuming, optimistically, everything I've said so far is true, I want to know how I would best go about deallocating the memory for all the Objs I've created. Clearing up Obj_Arr above can be done with:
delete[] Obj_Arr;



But would the same syntax work for Obj_PtrArr or would it only deallocate the memory of the five pointers? It seems to me that the actual objects are buried one level deeper and so I'm skeptical that the same keyword would do the job. Does it suffice? Have I got the right idea about any of this? I realise I've asked a lot; I'm hoping to nail down my understanding of this but the tutorials I find online, while generally good, don't go into real detail about what's going on. Perhaps it's for the best, as it might put people off early. If anyone is willing to offer any advice, I'd really appreciate it. Thanks a lot, Goldstein.

Share this post


Link to post
Share on other sites
Deleting an array of ordinary pointers will only delete the pointers and not the things they point to. Without knowing exactly what you're doing it's hard to say what you should be doing, but dealing with raw pointers is usually a suboptimal method of dealing with memory management. Better options usually include using smart pointers or storing the objects in actual container classes like std::list, std::vector and so on.

Share this post


Link to post
Share on other sites
To delete the objects referenced by the pointer you would need to iterate through the array and delete them individually, then you can safely delete the pointer array itself.

This isn't a very good way of managing memory though, you might consider one of these however:

- A static array of objects
Object objects[5];

- A dynamic collection of objects
std::vector<Object> objects;

- A container of smart pointers
std::vector< boost::shared_ptr<Object> > objects;

- A container of managed pointers
boost::ptr_vector<Object> objects;

That last option is most similar to what you're trying to do, but it's been implemented by professionals for maximum efficiency and tested thoroughly to ensure cross compiler portability.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this