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

Started by
22 comments, last by Emmanuel Deloget 17 years, 9 months ago
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,
Dave.
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.
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
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,
Dave.
[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]
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;}
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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
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]
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]
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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();
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V

This topic is closed to new replies.

Advertisement