Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > deleting all elements in a STL container of pointers

Reply
Thread Tools

deleting all elements in a STL container of pointers

 
 
edward.birch@gmail.com
Guest
Posts: n/a
 
      12-17-2005
Can anyone see anything wrong with the following code?

(CONTAINER can be list, vector, set, ...)

template <class T> void Destroy(T * p) { delete p; }

void CleanUp(std::CONTAINER<ContainerType *> & Container)
{
std::foreach(Container.begin(), Container.end(),
Destroy<ContainerType>);
Container.clear();
}

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?St=E9phane_Wirtel?=
Guest
Posts: n/a
 
      12-17-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Can anyone see anything wrong with the following code?
>
> (CONTAINER can be list, vector, set, ...)
>
> template <class T> void Destroy(T * p) { delete p; }
>
> void CleanUp(std::CONTAINER<ContainerType *> & Container)
> {
> std::foreach(Container.begin(), Container.end(),
> Destroy<ContainerType>);
> Container.clear();
> }
>

Sorry for my english,

In your Cleanup function, where do you define the formal parameter of
your template ?

To erase a item collection, you can use a functor.
Look my struct template 'Destroy', with this declaration (and definition).

template <typename T> struct Destroy {
void operator () (T pointer) {
std::cout << "Destroy of this pointer" << std::endl;
delete pointer;
pointer = 0;
}
};

Here, you must to define the type of your container :
The type is detected by the instantiation of your template.

template <typename Container> void CleanUp (Container & pContainer) {
std::for_each (
pContainer.begin (),
pContainer.end (),
Destroy<typename Container::value_type> ());
}

in your main function :

int main (int argc, char **argv) {
std::vector <std::string *> vect;
vect.push_back (new std::string ("Stephane"));
CleanUp (vect);
}

Best regards,

Stephane
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?St=E9phane_Wirtel?=
Guest
Posts: n/a
 
      12-17-2005
(E-Mail Removed) wrote:
> Can anyone see anything wrong with the following code?
>
> (CONTAINER can be list, vector, set, ...)
>
> template <class T> void Destroy(T * p) { delete p; }
>
> void CleanUp(std::CONTAINER<ContainerType *> & Container)
> {
> std::foreach(Container.begin(), Container.end(),
> Destroy<ContainerType>);
> Container.clear();
> }
>

About this subject, I have good advice for you.

Buy the book : "C++ Templates : The Complete Guide"
Product Details

* Hardcover: 552 pages
* Publisher: Addison-Wesley Professional; 1st edition (November 12,
2002)
* Language: English
* ISBN: 0201734842
* Product Dimensions: 9.5 x 7.5 x 1.2 inches
 
Reply With Quote
 
Mateusz Loskot
Guest
Posts: n/a
 
      12-17-2005
I'd also add Scott Meyers' "Effective STL" and Item 7.

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net

 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      12-18-2005

(E-Mail Removed) wrote:
> Can anyone see anything wrong with the following code?
>
> (CONTAINER can be list, vector, set, ...)
>
> template <class T> void Destroy(T * p) { delete p; }
>
> void CleanUp(std::CONTAINER<ContainerType *> & Container)
> {
> std::foreach(Container.begin(), Container.end(),
> Destroy<ContainerType>);
> Container.clear();
> }


I recommend you use smart pointers in your container instead of raw
pointers.
If you use smart pointers, then you don't have to worry about
explicitly deleting the pointers, because the smart pointer will do
that for you.
You can use the boost::shared_ptr or clone smart pointers like copy_ptr
and cow_ptr.

http://www.boost.org/libs/smart_ptr/shared_ptr.htm

http://code.axter.com/copy_ptr.h
http://code.axter.com/cow_ptr.h

Example usage:
std::vector<boost::shared_ptr<foo> > vFoo;

std::vector<copy_ptr<foo> > vFoo;

 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      12-18-2005

(E-Mail Removed) wrote:
> Can anyone see anything wrong with the following code?
>
> (CONTAINER can be list, vector, set, ...)
>
> template <class T> void Destroy(T * p) { delete p; }
>
> void CleanUp(std::CONTAINER<ContainerType *> & Container)
> {
> std::foreach(Container.begin(), Container.end(),
> Destroy<ContainerType>);
> Container.clear();
> }


The problem is that there can be a problem using Destroy<ContainerType>
as a template function. Although you can use a function in this
situation, it doesn't work with template functions.

So you should use a functor as other posters have suggested.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extending an stl container and iterator to also find groups of elements alan.f22@gmail.com C++ 2 08-18-2007 07:19 PM
container inside container in stl wolverine C++ 2 07-24-2006 03:08 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
Does deleting a container of pointers also delete the (contained) pointers? Xamalek C++ 7 11-04-2003 04:17 PM



Advertisments