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

A

Allerdyce.John

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();
}
 
V

Victor Bazarov

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
 
A

Allerdyce.John

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)?
 
V

Victor Bazarov

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
 
A

Axter

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.
 
A

Allerdyce.John

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 said:
.../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?
 
M

Marcus Kwok

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);

?
 
C

Csaba

(e-mail address removed) wrote in @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 );
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top