STL : list, map, ...

V

Vincent RICHOMME

Hi,

I have a question about STL lis, map...
Let's say I have declared a list of pointer to object, when I call clear
does it free all my objects ?

Ex :
typedef list<CGUIElement*> CGUIElementList;
CGUIElement* pGUIElement = NULL;
CGUIElementList m_GUIElements;



pGUIElement = new CGUIElement();
m_GUIElements.push_back( pGUIElement );

pGUIElement = new CGUIElement();
m_GUIElements.push_back( pGUIElement );


GUIElements.clear(); // Does it free memory ?
 
V

Victor Bazarov

Vincent said:
I have a question about STL lis, map...
Let's say I have declared a list of pointer to object, when I call clear
does it free all my objects ?

Why would it? What if you put addresses of static objects there?
Ex :
typedef list<CGUIElement*> CGUIElementList;
CGUIElement* pGUIElement = NULL;
CGUIElementList m_GUIElements;



pGUIElement = new CGUIElement();
m_GUIElements.push_back( pGUIElement );

pGUIElement = new CGUIElement();
m_GUIElements.push_back( pGUIElement );


GUIElements.clear(); // Does it free memory ?

It only frees the memory 'GUIElements' allocated for itself.

V
 
A

Axter

Vincent said:
Hi,

I have a question about STL lis, map...
Let's say I have declared a list of pointer to object, when I call clear
does it free all my objects ?

Ex :
typedef list<CGUIElement*> CGUIElementList;
CGUIElement* pGUIElement = NULL;
CGUIElementList m_GUIElements;



pGUIElement = new CGUIElement();
m_GUIElements.push_back( pGUIElement );

pGUIElement = new CGUIElement();
m_GUIElements.push_back( pGUIElement );


GUIElements.clear(); // Does it free memory ?

No.
But you can have it clean up the memory automatically by using a smart
pointer like boost::shared_ptr or a clone smart pointer like copy_ptr &
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

You should avoid using raw pointers with STL containers, and instead
use smart pointers which are safer in general.
 
K

KK

I've question based on this discussion.
What is the answer in this case?
vector< vector <string> > ex;
/* push back some objects into the 'ex' container */
ex.clear()
does the above command could free memory?
 
B

BobR

KK wrote in message
I've question based on this discussion.
What is the answer in this case?
vector< vector <string> > ex;
/* push back some objects into the 'ex' container */
ex.clear()
does the above command could free memory?


std::vector<std::vector<std::string> > ex(1,1);
/* **push_back** some objects into the 'ex' container */
std::cout << "\n ex.size() == "<<ex.size()<<std::endl;
for(size_t a(0); a < ex.size(); ++a){
std::cout << "ex.at(" << a << ")";
for(size_t b(0); b < ex.at(a).size(); ++b){
std::cout<<".at(" << b << ") =="<<ex.at(a).at(b)<<std::endl;
} //for(b)
std::cout << "\n";
} // for(a)
std::cout<<"__________"<<std::endl;
ex.clear()
std::cout << " After clear. ex.size() == "<<ex.size()<<std::endl;

Cut and paste that into main()/(a function) and tell us what output you get.
 
J

Jim Langston

KK said:
I've question based on this discussion.
What is the answer in this case?
vector< vector <string> > ex;
/* push back some objects into the 'ex' container */
ex.clear()
does the above command could free memory?

Yes. When you clear() a vector the vector will call the dtor for each
object itself.

vector< int > ex;
ex.clear(); // The memory the ints take up is deleted.

vector< int* > ex;
ex.clear(); // the memory the int pointers take up is deleted.
// just not what the int pointers were pointing to.

so your vector< std::string > the std::strings will be deleted when each
vector is.

As long as you're not using new or malloc to allocate your own memory, you
should be fine.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top