Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Free memory allocate by a STL vector, vector of vector, map of vector

Reply
Thread Tools

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

 
 
Allerdyce.John@gmail.com
Guest
Posts: n/a
 
      02-17-2006
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();
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-17-2006
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
 
Reply With Quote
 
 
 
 
Allerdyce.John@gmail.com
Guest
Posts: n/a
 
      02-17-2006
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)?

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-17-2006
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
 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      02-17-2006
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.

 
Reply With Quote
 
Allerdyce.John@gmail.com
Guest
Posts: n/a
 
      02-17-2006
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?

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      02-17-2006
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
 
Reply With Quote
 
Allerdyce.John@gmail.com
Guest
Posts: n/a
 
      02-17-2006
Thanks.

 
Reply With Quote
 
Csaba
Guest
Posts: n/a
 
      02-18-2006
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.
 
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
Make STL containers allocate aligned memory zr C++ 8 11-27-2008 11:51 AM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
Is Vector Reserve guaranteed to allocate contiguous memory? Gary Kuehn C++ 2 07-19-2005 12:42 AM
How to allocate mem without using malloc() & free without using free() Rajshekhar C Programming 5 03-29-2005 06:03 PM
Re: FileChannel.map() gives 'cannot allocate memory' Roedy Green Java 3 08-14-2003 08:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57