Jump to content
  • Advertisement
Sign in to follow this  
DangerDave

Technique for destroying an array of non-unique objects (C++)?

This topic is 4427 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an array filled with pointers to objects on the heap. Some of the elements in the array point to the same object as others. The obvious problem when cleaning up then is in cycling through the array and 'delete'-ing each unique object, without deleting any more than once... The only way I can think to do this is to construct a new array of unique pointers using a few nested for-loops (to scan through the original array and scan through a new array, checking and inserting), then scan through the new array deleting each element. Anything more elegant much appreciated....? Cheers,

Share this post


Link to post
Share on other sites
Advertisement
I suggest you look into smart pointers, such as boost::shared_ptr.

You could also delete an item, add the address in a list set, and ignore any future pointer with an address that is in that list set. [edit: which amounts to building a list of unique pointers, meh.]

[edit2: I wonder if ordering the list first, then deleting and skipping duplicates would be more efficient. Worth a try.]


jfl.

Share this post


Link to post
Share on other sites
It really depends on what exactally you are aiming for, if the reason you don't want to create another array is due to memory concerns, you could always sort first, or just delete one pointer every iteration of the list... Not really any very elegant way to do it.

Indy

Share this post


Link to post
Share on other sites
Ah, ok. Thats settled then, there is no better way. Not so bothered about memory/efficiency, just like to make sure my code is as elegant as possible.

Thanks for the swift replies,

Share this post


Link to post
Share on other sites
[edit: read Fruny's response]
[edit2: the iterators should be double pointers]
The STL provides unique(), which is required to be of linear complexity. Use would be something like:

Item *array[20];
// ...

Item **end = std::unique( array, array + 20 );
for( Item **it = array; it != end; ++it ) {
delete *it;
}
// clear array


[Edited by - jflanglois on July 4, 2006 2:37:56 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by jflanglois
The STL provides unique(), which is required to be of linear complexity.


Please be warned that the sequence needs to be sorted first. unique() only eliminates adjacent duplicates.

Item *array[20];
// ...

std::sort(array, array + 20);
Item *end = std::unique( array, array + 20 );
for( Item *it = array; it != end; ++it ) {
delete it;
}

Share this post


Link to post
Share on other sites
Though, if it's a relatively small list, I would probally iterate over the list once per every object I have to delete, Null every one I delete, then iterate again, from the next index I didn't null, Mostly just to save on the overhead of the allocation and destruction of the second list. But for no real preformance reasons.

Indy

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by jflanglois
The STL provides unique(), which is required to be of linear complexity.


Please be warned that the sequence needs to be sorted first. unique() only eliminates adjacent duplicates.

Item *array[20];
// ...

std::sort(array, array + 20);
Item *end = std::unique( array, array + 20 );
for( Item *it = array; it != end; ++it ) {
delete it;
}

Exactly right. My mistake.
Quote:
unique()
Every time a consecutive group of duplicate elements ...


[Edited by - jflanglois on July 4, 2006 2:42:03 AM]

Share this post


Link to post
Share on other sites
Surely the most appropriate thing to do here is to store reference-counted smart pointers in the array, instead of raw pointers?!?!
Then you simply delete them all![cool]

Share this post


Link to post
Share on other sites
Or better yet, assuming you have control over the array....

std::vector< boost::shared_ptr<Item> > array;

// do stuff with array, insert pointers, etc.
//...

// Surely this is too easy to be true!!!
array.clear();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!