deleting all elements in a STL container of pointers

Discussion in 'C++' started by edward.birch@gmail.com, Dec 17, 2005.

  1. Guest

    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();
    }
    , Dec 17, 2005
    #1
    1. Advertising

  2. 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
    =?ISO-8859-1?Q?St=E9phane_Wirtel?=, Dec 17, 2005
    #2
    1. Advertising

  3. 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
    =?ISO-8859-1?Q?St=E9phane_Wirtel?=, Dec 17, 2005
    #3
  4. Mateusz Loskot, Dec 17, 2005
    #4
  5. Axter Guest

    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;
    Axter, Dec 18, 2005
    #5
  6. Earl Purple Guest

    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.
    Earl Purple, Dec 18, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xamalek
    Replies:
    7
    Views:
    685
  2. Maitre Bart
    Replies:
    2
    Views:
    521
    Maitre Bart
    Feb 11, 2004
  3. Replies:
    4
    Views:
    798
    Daniel T.
    Feb 16, 2006
  4. Varun  Kacholia
    Replies:
    1
    Views:
    315
    Pete Becker
    Apr 1, 2006
  5. wolverine
    Replies:
    2
    Views:
    450
    Marcus Kwok
    Jul 24, 2006
Loading...

Share This Page