473,474 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

STL : list, map, ...

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
6 1744
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
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
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
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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesn¹t matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
0
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.