473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 21416
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_ptr 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::Delete ? 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
I mean that "place" in memory where pointer (address) is stored is
cleaned up/cleared/erased.

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

Dec 17 '05 #11

"Mateusz Loskot" <ma*****@loskot.net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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).


His statement was now wrong, you just didn't like how it was phrased :D I
understood what he was saying.
Dec 18 '05 #12
Jim Langston napisał(a):
"Mateusz Loskot" <ma*****@loskot.net> wrote in message

*** - 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).

His statement was now wrong
[...]


Why?

Cheers

--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 19 '05 #13
>I mean that "place" in memory where pointer (address) is stored is
cleaned up/cleared/erased.


Now you are not being precise.

What do mean that the "pointer is cleaned up/cleared/erased?"
What _exactly_ happens?
Do you mean, for instance, that the pointer is set to NULL?

Please enlighten me. Thanks.

Stephen Howe
Dec 20 '05 #14
Stephen Howe wrote:
I mean that "place" in memory where pointer (address) is stored is
cleaned up/cleared/erased.

Now you are not being precise.

What do mean that the "pointer is cleaned up/cleared/erased?"
What _exactly_ happens?
Do you mean, for instance, that the pointer is set to NULL?

Please enlighten me. Thanks.


Hm, seems you've catched me.
I know how to _use_ it but I don't know exactly how it's implemented.
Setting it to NULL seems to not to be enough. Memory should albo be
deallocated.

I'd be glad to hear a deep explanation of this problem from soe Gurus.
Is there anyone who could explain those internals?

Cheers

--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 20 '05 #15
Mateusz Łoskot wrote:
Stephen Howe wrote:
I mean that "place" in memory where pointer (address) is stored is
cleaned up/cleared/erased.


Now you are not being precise.

What do mean that the "pointer is cleaned up/cleared/erased?"
What _exactly_ happens?
Do you mean, for instance, that the pointer is set to NULL?

Please enlighten me. Thanks.

Hm, seems you've catched me.
I know how to _use_ it but I don't know exactly how it's implemented.
Setting it to NULL seems to not to be enough. Memory should albo be
deallocated.

I'd be glad to hear a deep explanation of this problem from soe Gurus.
Is there anyone who could explain those internals?

Cheers


That's exactly the point-- you're fussing about implementation details
which aren't specified by (nor relevant to) the operation of the class.
(I'll concede that my original reply was not precise enough for legal
work, but I think the message was clear to everyone, particularly the
original poster.)

In my limited experience, maps are usually implemented in terms of red
black trees and the individual data stored in the map are wrapped up in
some sort of node structure which includes the key, the value, and some
additional information (for example, pointers to other nodes in the tree).

You'll get a sense for this if you write your own STL allocator because
you can then observe the individual memory allocations and
deallocations. If you have a map<int,int> for example, and sizeof(int)
= 4, it would be fairly typical to find that each insertion results in
an allocation of size, say, 24, which you can see is 2 ints plus "some
other stuff". This confirms the sometimes surprising fact that a map
passed an allocator<T> when constructed may very likely never allocate
space for a T. Instead the allocator rebind mechanism is used to
allocate space for some other object-- in this case a tree node.

In fact, to really throw a wrench into things (and return to your point
above), I've seen implementations (e.g. RogueWave) which use block
allocations so that as soon as one item is added to the map, space is
allocated for say 32 items. Conversely, it's possible to erase items
from such a map and have no memory immediately deallocated. To the
user the map must appear not to contain the erased items, but there may
be no memory deallocation until a whole block's worth of items have been
erased. Or for that matter, until the implementation feels like
deallocating the memory.

Mark

Dec 20 '05 #16
Mark P wrote:
Mateusz Łoskot wrote:
Stephen Howe wrote:
What do mean that the "pointer is cleaned up/cleared/erased?"
What _exactly_ happens?
Do you mean, for instance, that the pointer is set to NULL?

Please enlighten me. Thanks.
Hm, seems you've catched me.
I know how to _use_ it but I don't know exactly how it's implemented.
Setting it to NULL seems to not to be enough. Memory should albo be
deallocated.

I'd be glad to hear a deep explanation of this problem from soe Gurus.
Is there anyone who could explain those internals?


That's exactly the point-- you're fussing about implementation details
which aren't specified by (nor relevant to) the operation of the class.


Yes, definitely you are right.
You know, I always imagine "cleaning of pointers" similarly to cleaning
(freeing) memory occupied as by int, double.
Just as it is here presented by scopes:

{
int i = 0;
}

// here i is not known and seems to be freed

I imagine it similarly with points:

{
int* pi = 0;
}

// here 4 bytes occupied by pi (pointer) are cleaned (free)

Certainly, I'm completely not sure about if it's implementated that way.

[...]


Thanks for interesting explanation.

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 22 '05 #17
Mateusz Loskot wrote:
You know, I always imagine "cleaning of pointers" similarly to cleaning
(freeing) memory occupied as by int, double.
Just as it is here presented by scopes:

{
int i = 0;
}

// here i is not known and seems to be freed
Could be. It's just not name-able. If it was a "static int" it won't be
freed
but it won't be name-able outside the scope either. The fact that i is
invisible outside the scope allows optimizers to do fancy things, so
you
cant say exactly what happens.
I imagine it similarly with points:

{
int* pi = 0;
}

// here 4 bytes occupied by pi (pointer) are cleaned (free)


With pointers you have to distiguish between the pointer itself (pi)
and
the pointed-to (*pi). In your case, pi is a null pointer so there is no

pointed-to (yet). At the }, the memory used to hold the null pointer
(sizeof(int*) bytes) is reusable. The reuse will typically be by
another
line like double dd = 5.0; . Therefore, the compiler won't clean up
the bytes at the } but simply overwrites the old value if/when a new
variable is defined. That saves time.

if you had
void foo( int* pi ) {
pi = 0;
}
it would be assignment. pi is a copy of a pointer. The original pointer
can be found
in the caller of foo(). The assignment to pi sets it to the null
pointer. The original
pointer remains unchanged. So, if it pointed to an int, it will
continue to do so.
Now, I think it's obvious why going out of scope doesn't free the
pointed-to memory.

PS. an int often, but not always is 4 bytes. So is an int* - but not
every CPU
is 32/32 bits. C++ doesn't care, 0 is always precisely enough bits.

Dec 22 '05 #18
Mi*************@tomtom.com wrote:
Mateusz Loskot wrote:
You know, I always imagine "cleaning of pointers" similarly to cleaning
(freeing) memory occupied as by int, double.
Just as it is here presented by scopes:

{
int i = 0;
}

// here i is not known and seems to be freed

Could be. It's just not name-able. If it was a "static int" it won't be
freed
but it won't be name-able outside the scope either. The fact that i is
invisible outside the scope allows optimizers to do fancy things, so
you cant say exactly what happens.


I understand it now much better.
I imagine it similarly with points:

{
int* pi = 0;
}

// here 4 bytes occupied by pi (pointer) are cleaned (free)

With pointers you have to distiguish between the pointer itself (pi)
and the pointed-to (*pi).


I'm sure I understand the difference.
In your case, pi is a null pointer so there is no
pointed-to (yet).
Yes, but during this discussion I'm talking about pointer itself but not
objects pointed by pointer.
At the }, the memory used to hold the null pointer
(sizeof(int*) bytes) is reusable. The reuse will typically be by
another line like double dd = 5.0; .
Yes, and that seems to be also what I tried to say writting "cleaning
pointer".
Therefore, the compiler won't clean up
the bytes at the } but simply overwrites the old value if/when a new
variable is defined.
That saves time.
[...]
And that explains everything. I wasn't sure exactly what compiler is
supposed to do with memory occupied by pointer itself.

PS. an int often, but not always is 4 bytes. So is an int* - but not
every CPU
is 32/32 bits. C++ doesn't care, 0 is always precisely enough bits.


Yes.

Thanks for clear explanation.
Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 22 '05 #19

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

Similar topics

0
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,...
1
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...
2
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...
7
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...
8
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)...
1
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...
0
by: erez_acount | last post by:
***************************************************************************** Call For Papers The 2006 International Symposium on Memory Management (ISMM'06) Co-located with PLDI 2006 ...
2
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...
2
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,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.