Free memory allocate by a STL vector, vector of vector, map of vector

Discussion in 'C++' started by Allerdyce.John@gmail.com, Feb 17, 2006.

  1. Guest

    Hi,

    I have the following code which frees memory allocate by a STL vector,
    vector of vector, map of vector.
    I would like to know if I indeed free all the memory (i.e. no memory
    leak).

    Thanks for any advice:
    /* From Effective STl */
    struct DeleteObject {

    template<typename T>
    void operator()(const T* ptr) const {
    delete ptr;
    }
    };

    typedef vector<A*> AVector;
    // free a vector of 'A*'
    void f4(AVector& aVector) {
    for_each(aVector.begin(), aVector.end(), DeleteObject());
    aVector.clear();
    }


    // free a vector of vector of 'A*'
    void f4(vector<AVector*>& aVectorVector) {
    for(vector<AVector*>::iterator itr = aVectorVector.begin(); itr !=
    aVectorVector.end(); ++itr) {
    AVector* aVector = (*itr);
    f4( *aVector);
    }
    aVectorVector.clear();
    }

    // free a map of vector of 'A*'
    void f4(map<int, AVector*>& aMapVector) {
    for(map<int, AVector*>::iterator itr = aMapVector.begin(); itr !=
    aMapVector.end(); ++itr) {
    AVector* aVector = (*itr).second;
    f4( *aVector);
    }
    aMapVector.clear();
    }
    , Feb 17, 2006
    #1
    1. Advertising

  2. Re: Free memory allocate by a STL vector, vector of vector, map ofvector

    wrote:
    > I have the following code which frees memory allocate by a STL vector,
    > vector of vector, map of vector.
    > I would like to know if I indeed free all the memory (i.e. no memory
    > leak).
    >
    > [...]


    Seems fine. Why do you ask? It would be better to use some kind of
    automatic memory leak detector (Purify, e.g.) instead of a newsgroup...

    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Feb 17, 2006
    #2
    1. Advertising

  3. Guest

    Thanks. Unfortantely I don't have budget to buy commerical memory leak
    tool.

    A separate question:
    How can I replace this with a for_each () algorithm?
    void f4(vector<AVector*>& aVectorVector) {
    for(vector<AVector*>::iterator itr = aVectorVector.begin(); itr
    !=
    aVectorVector.end(); ++itr) {
    AVector* aVector = (*itr);
    f4( *aVector);
    }
    aVectorVector.clear();


    the f4() takes a Reference, but my Vector of Vector is a pointer, how
    can I deference that pointer before calling void f4(AVector& aVector)?
    , Feb 17, 2006
    #3
  4. Re: Free memory allocate by a STL vector, vector of vector, map ofvector

    wrote:
    > Thanks. Unfortantely I don't have budget to buy commerical memory leak
    > tool.


    Use a free one, like 'valgrind'...

    > A separate question:
    > How can I replace this with a for_each () algorithm?
    > void f4(vector<AVector*>& aVectorVector) {
    > for(vector<AVector*>::iterator itr = aVectorVector.begin(); itr
    > !=
    > aVectorVector.end(); ++itr) {
    > AVector* aVector = (*itr);
    > f4( *aVector);
    > }


    If you change your f4, then

    for_each(aVectorVector.begin(), aVectorVector.end(), f4);

    > aVectorVector.clear();
    >
    >
    > the f4() takes a Reference, but my Vector of Vector is a pointer, how
    > can I deference that pointer before calling void f4(AVector& aVector)?


    Make your 'f4' take [a reference to] a pointer. Don't forget to check for
    NULL in it.

    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Feb 17, 2006
    #4
  5. Axter Guest

    wrote:
    > Hi,
    >
    > I have the following code which frees memory allocate by a STL vector,
    > vector of vector, map of vector.
    > I would like to know if I indeed free all the memory (i.e. no memory
    > leak).
    >
    > Thanks for any advice:
    > /* From Effective STl */
    > struct DeleteObject {
    >
    > template<typename T>
    > void operator()(const T* ptr) const {
    > delete ptr;
    > }
    > };
    >
    > typedef vector<A*> AVector;
    > // free a vector of 'A*'
    > void f4(AVector& aVector) {
    > for_each(aVector.begin(), aVector.end(), DeleteObject());
    > aVector.clear();
    > }
    >
    >
    > // free a vector of vector of 'A*'
    > void f4(vector<AVector*>& aVectorVector) {
    > for(vector<AVector*>::iterator itr = aVectorVector.begin(); itr !=
    > aVectorVector.end(); ++itr) {
    > AVector* aVector = (*itr);
    > f4( *aVector);
    > }
    > aVectorVector.clear();
    > }
    >
    > // free a map of vector of 'A*'
    > void f4(map<int, AVector*>& aMapVector) {
    > for(map<int, AVector*>::iterator itr = aMapVector.begin(); itr !=
    > aMapVector.end(); ++itr) {
    > AVector* aVector = (*itr).second;
    > f4( *aVector);
    > }
    > aMapVector.clear();
    > }


    It's better and safer to use a smart pointer for this, than to use this
    type of manual mememory management.
    Anytime you have to make explicit calls to clear your memory, you're
    only asking for memory leaks.

    Consider using boost::shared_ptr, or one of the following smart
    pointers:
    http://code.axter.com/cow_ptr.h
    http://code.axter.com/copy_ptr.h
    http://code.axter.com/smart_ptr.h

    vector<cow_ptr<AVector> > MyvA;
    vector<smart_ptr<AVector> > MyvA;
    vector<boost::shared_ptr<AVector> > MyvA;


    The code is free, and so as boost code, and you don't have to worry
    about memory leaks.
    Axter, Feb 17, 2006
    #5
  6. Guest

    Thanks for the tip.

    A related question, I would like to convert my f4() into template:
    so I try this:
    template <class T>
    void f5(T* aVector) {
    if (aVector != NULL) {
    for_each(aVector.begin(), aVector.end(), DeleteObject());
    aVector.clear();
    }
    }


    template <class T>
    void f5(vector<T*>& aVectorVector) {
    for_each( T.begin(), T.end(), f5);
    aVectorVector.clear();
    }

    But I get error like this:
    .../Dan2.cpp: In function 'void f5(std::vector<T*, std::allocator<T*>
    >&)':

    .../Dan2.cpp:72: error: expected primary-expression before '.' token
    .../Dan2.cpp:72: error: expected primary-expression before '.' token
    .../Dan2.cpp:72: error: no matching function for call to
    'for_each(<type error>, <type error>, <unknown type>)'

    Can you please tell me how can i try that into a template?
    , Feb 17, 2006
    #6
  7. Marcus Kwok Guest

    wrote:
    > Thanks for the tip.
    >
    > A related question, I would like to convert my f4() into template:
    > so I try this:
    > template <class T>
    > void f5(T* aVector) {
    > if (aVector != NULL) {
    > for_each(aVector.begin(), aVector.end(), DeleteObject());
    > aVector.clear();
    > }
    > }
    >
    >
    > template <class T>
    > void f5(vector<T*>& aVectorVector) {
    > for_each( T.begin(), T.end(), f5);


    Should this be:

    for_each(aVectorVector.begin(), aVectorVector.end(), f5);

    ?

    > aVectorVector.clear();
    > }
    >
    > But I get error like this:
    > ../Dan2.cpp: In function 'void f5(std::vector<T*, std::allocator<T*>
    >>&)':

    > ../Dan2.cpp:72: error: expected primary-expression before '.' token
    > ../Dan2.cpp:72: error: expected primary-expression before '.' token
    > ../Dan2.cpp:72: error: no matching function for call to
    > 'for_each(<type error>, <type error>, <unknown type>)'
    >
    > Can you please tell me how can i try that into a template?
    >


    --
    Marcus Kwok
    Marcus Kwok, Feb 17, 2006
    #7
  8. Guest

    Thanks.
    , Feb 17, 2006
    #8
  9. Csaba Guest

    wrote in news:1140204299.978888.253590
    @g44g2000cwa.googlegroups.com:

    > Thanks for the tip.
    >
    > A related question, I would like to convert my f4() into template:
    > so I try this:
    > template <class T>
    > void f5(T* aVector) {
    > if (aVector != NULL) {
    > for_each(aVector.begin(), aVector.end(), DeleteObject());
    > aVector.clear();
    > }
    > }
    >
    >
    > template <class T>
    > void f5(vector<T*>& aVectorVector) {
    > for_each( T.begin(), T.end(), f5);


    I assume the above is line 72.

    T is a type name, not an object, so you can't use the member selection
    operator on it. Maybe you meant

    for_each( aVectroVector.begin(), aVectroVector.end(), f5 );

    > aVectorVector.clear();
    > }
    >
    > But I get error like this:
    > ../Dan2.cpp: In function 'void f5(std::vector<T*, std::allocator<T*>
    >>&)':

    > ../Dan2.cpp:72: error: expected primary-expression before '.' token
    > ../Dan2.cpp:72: error: expected primary-expression before '.' token
    > ../Dan2.cpp:72: error: no matching function for call to
    > 'for_each(<type error>, <type error>, <unknown type>)'
    >
    > Can you please tell me how can i try that into a template?
    >
    >




    --
    Life is complex, with real and imaginary parts.
    Csaba, Feb 18, 2006
    #9
    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. Roedy Green
    Replies:
    3
    Views:
    3,232
    Roedy Green
    Aug 14, 2003
  2. Rajshekhar
    Replies:
    5
    Views:
    2,108
    Jonathan Bartlett
    Mar 29, 2005
  3. Gary Kuehn
    Replies:
    2
    Views:
    443
    Gary Kuehn
    Jul 19, 2005
  4. zr
    Replies:
    8
    Views:
    4,653
    Maxim Yegorushkin
    Nov 27, 2008
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    317
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page