By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

STL : list, map, ...

P: n/a
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 ?
Dec 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Le Fri, 23 Dec 2005 16:56:09 +0100, Vincent RICHOMME a écrit*:

Hi,

Nope, cleaning up the memory is the responsibility of the user.

http://gcc.gnu.org/onlinedocs/libstd...l#std_1_1mapa4

Martin
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 ?

Dec 23 '05 #2

P: n/a
Vincent RICHOMME wrote:
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
Dec 23 '05 #3

P: n/a
Vincent RICHOMME wrote:
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.

Dec 23 '05 #4

P: n/a
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?

Victor Bazarov wrote:
Vincent RICHOMME wrote:
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


Dec 24 '05 #5

P: n/a

KK wrote in message
<11**********************@o13g2000cwo.googlegroups .com>...
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.

--
Bob R
POVrookie
Dec 24 '05 #6

P: n/a

"KK" <ke*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.
Dec 26 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.