Technique for destroying an array of non-unique objects (C++)?
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,
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.
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.
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
Indy
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,
Thanks for the swift replies,
[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:
[Edited by - jflanglois on July 4, 2006 2:37:56 AM]
[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]
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;}
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
Indy
Quote:Original post by FrunyQuote: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]
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]
Then you simply delete them all![cool]
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();
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement