473,748 Members | 10,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL map and memory management (clear() )

Jan
Hi there,
i've got an STL map with something like this ( map<string, Object*>
xyz; )
What happens when I call xyz.clear()?
Is only the map cleared or the map and the Objects, so that the memory
is free again?
Have I to delete every single Object?

Thanks for replying,
Jan

Dec 15 '05 #1
18 21584
Jan wrote:
Hi there,
i've got an STL map with something like this ( map<string, Object*>
xyz; )
What happens when I call xyz.clear()?
Is only the map cleared or the map and the Objects, so that the memory
is free again?
Have I to delete every single Object?

Thanks for replying,
Jan

clear() empties out the contents of the map. It doesn't do anything
with the Object*; in particular it doesn't delete them.
Dec 15 '05 #2
Jan
thanks :-)

Dec 15 '05 #3
Jan wrote:
Hi there,
i've got an STL map with something like this ( map<string, Object*>
xyz; )
What happens when I call xyz.clear()?
Is only the map cleared or the map and the Objects, so that the memory
is free again?
Have I to delete every single Object?

Thanks for replying,
Jan


None of the STL containers will delete a pointer type.
I recommend you use smart pointers instead of raw pointers with STL
containers.
You can not use auto_ptr with STL containers, but you can use other
common smart pointers like boost::shared_p tr and clone pointers like
copy_ptr and 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

Dec 15 '05 #4
Mark P wrote:

It doesn't do anything with the Object*; in particular it doesn't delete them.


Wrong!
Object* is a pointer or more descriptively - it is a pointer type to
objects of type of Object.
Every STL container destroys its elements while being destroyed itself.
So, when you call std::map::clear () then all contained pointers will be
destroyed, BUT NOT objects pointed by them. All those objects will stay
alive in memory.
In your case, container-of-pointers, no STL container will delete
instances of Object class for you because it stores pointers, not
objects. So, you have to destroy those instances by yourself: iterated
through std::map elements and call delete on contained pointer.

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net

Dec 15 '05 #5
I'd like to complement my last post.
Currently, I'm reading amazing book "C++ Gotchas: Avoiding Common
Problems in Coding and Design" by Stephen C. Dewhurst.
There is "Gotcha #83: Failure to Distinguish Aggregation and
Acquaintance" chapter which, what at first sight may seem, does not
drift a-way from the subject.

It is really woth to read, especially when you are working with
containers-of-pointers.

At the end, author express' very neatly - in one sentence - what I
wanted to say to my previous post:

"For pointer elements, the standard* containers will clean up the
pointers but not the objects to which they refer."

* - means STL containers
Cheers
--
Mateusz Loskot
http://mateusz.loskot.net

Dec 16 '05 #6
> Wrong!
Object* is a pointer or more descriptively - it is a pointer type to
objects of type of Object.
Every STL container destroys its elements while being destroyed itself.
So, when you call std::map::clear () then all contained pointers will be
destroyed...


How is Mark P wrong?
A pointer has no destructor, so clear() has nothing to do for each value
element.

Stephen Howe
Dec 16 '05 #7
I said this sentence is wrong: "It doesn't do anything with the
Object*".
OK maybe not wrong, but not precise.

Your sentence "has nothing to do" is also not precise.
Why? Pointers are "cleaned up"*** after std::map::clear () is called.

*** - See my previous post where I quoted Stephen C. Dewhurst

If there "nothing is done" there also pointers would not be cleared,
pointers are data, they occupy space in memory, just as integers or
objects. So, they must be cleaned (we can say "destroyed" and it won't
be mistake).

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net

Dec 16 '05 #8

Jan wrote:
Hi there,
i've got an STL map with something like this ( map<string, Object*>
xyz; )
What happens when I call xyz.clear()?
Is only the map cleared or the map and the Objects, so that the memory
is free again?
Have I to delete every single Object?


Depends. If the Object* are non-owning (e.g. because xyx is just a way
of associating names with list<Object> entries), no. The list<Object>
will get them. However, if the Object* are owning, then yes of course.

Besides, how would the STL know whether to call delete or delete[]
or &Object::Del ete ? And what if it wasn't Object* but char*?

HTH,
Michiel Salters

Dec 16 '05 #9
> Your sentence "has nothing to do" is also not precise.
Why? Pointers are "cleaned up"*** after std::map::clear () is called.

If there "nothing is done" there also pointers would not be cleared,
pointers are data, they occupy space in memory, just as integers or
objects. So, they must be cleaned (we can say "destroyed" and it won't
be mistake).


Your not being precise.
What do you mean by pointers must be cleaned?
What does "cleaning" involve?

Stephen Howe

Dec 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1878
by: Richard Jones | last post by:
Garbage Collection & Memory Management Summer School 20-21 July 2004, Canterbury, UK The performance of today's memory-hungry applications depends on efficient dynamic memory management, whether managed explicitly through new/delete or automatically by a garbage collector. With the increasing use of managed code, whether Java Virtual Machines or Microsoft's Common Language Runtime, the economic importance of automatic memory management...
1
1392
by: Dave | last post by:
Hello all, I am creating arbitrary trees and would like to not have to deal with memory management. The approach I've taken is to put each node in a std::list<>. Obviously, the nodes will be deallocated when the list is destroyed or when I call clear(). This approach works because my nodes can have simple pointers to parent / children / siblings since the elements in a list never move. Can anybody see any shortcomings in this approach...
2
1965
by: DANIEL BEAULIEU J | last post by:
Basically i am a student taking an operating systems course which is c++ intensive. Familiar with Java, and so not so familiar with memory management. Looking for suggestions of exercises or web resources to help me become familiar with pointers and referencing etc. Thank you.
7
1856
by: Dan Nilsen | last post by:
Hi! I'm writing a small piece of software that basically runs on an embedded system with a Power-PC cpu. This runs on a stripped down version of Linux - Busybox. As I'm writing a piece of code that basically acts as a server and that will be running for weeks or months and probably even longer, memory management is a topic that is quite crucial.
8
9332
by: Chad | last post by:
hello, i am losing memory each time i make this call to a C++ dll (I make frequent calls). 'in VB.Net Declare Function grab Lib "grabber.dll" _ (ByRef lpBuf As Byte, ByVal lnum As Integer) As Integer the C++ function is...
1
2799
by: trialproduct2004 | last post by:
Hi all, I am having slight confusion regarding memory management in .net. Say suppose i have two application one is in C# and other is in MFC(VC++). Both of this application are using lots of memory. Suppose i run first C# application which has occupied all memory and
0
1935
by: erez_acount | last post by:
***************************************************************************** Call For Papers The 2006 International Symposium on Memory Management (ISMM'06) Co-located with PLDI 2006 Ottawa, Canada June 10-11 2006
2
2376
by: Epetruk | last post by:
Hello, I have a problem where an application I am working on throws an OutOfMemoryException when I run it in Release mode, whereas it doesn't do this when I am running in Debug. The application is developed using C++/Managed C++ and built using VS 2003 under .NET framework 1.1. In Debug, it uses of up to 600Mb of memory, whereas in Release it only gets
2
1460
by: Daniel Pitts | last post by:
I'm trying decide on the best way to structure the memory management in my program. I have a class (lets call it World), which contains a collection of Entity objects. Entity in turn, contains a pointer a Shape object. Shape is a pure virtual class. Now, I'm not sure who should "own" the allocation and deallocation of
0
9537
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9243
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.