473,803 Members | 3,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

clear() before delete? --hash_map

For STL containers like hash_map, need I explicitly call clear()
before delete()ing it?

e.g.
hash_map<string , MyClass**my_map ;

my_map = new (hash_map<strin g, MyClass*>);

....
....

for(hash_map<My Class*>::iterat or it = my_map->begin() ; it != my_map-
>end(); ++it)
delete it->second;
// do I need call my_map->clear()?
delete my_map;

Thank,

Mar 28 '07 #1
4 3215
newbie wrote:
For STL containers like hash_map, need I explicitly call clear()
before delete()ing it?
hash_map is, AFAIK, not yet standard. Nonetheless the answer to your
question is "no".
>
e.g.
hash_map<string , MyClass**my_map ;

my_map = new (hash_map<strin g, MyClass*>);

...
...

for(hash_map<My Class*>::iterat or it = my_map->begin() ; it != my_map-
>end(); ++it)
delete it->second;
// do I need call my_map->clear()?
delete my_map;

Thank,
Mar 28 '07 #2
newbie wrote:
For STL containers like hash_map, need I explicitly call clear()
before delete()ing it?

e.g.
hash_map<string , MyClass**my_map ;

my_map = new (hash_map<strin g, MyClass*>);

[redacted]

delete my_map;

Any particular reason you're new-ing this, instead of declaring it:

hash_map<string , MyClass*my_map;

In general, you don't need to use new and delete as often as most people
seem to think.
Mar 28 '07 #3
On Mar 27, 8:25 pm, red floyd <no.s...@here.d udewrote:
newbie wrote:
For STL containers like hash_map, need I explicitly call clear()
before delete()ing it?
e.g.
hash_map<string , MyClass**my_map ;
my_map = new (hash_map<strin g, MyClass*>);
[redacted]
delete my_map;

Any particular reason you're new-ing this, instead of declaring it:

hash_map<string , MyClass*my_map;

In general, you don't need to use new and delete as often as most people
seem to think.
does hash_map<string , MyClass*my_map; (which I am not sure how many
byte it needs)
use more memory on stack, suppose the class is initialized as a stack
variable,
than "hash_map<strin g, MyClass**my_map ;" (which is just a pointer)?

Thanks,

Apr 6 '07 #4
On Apr 6, 7:45 pm, "newbie" <mitbb...@yahoo .comwrote:
does hash_map<string , MyClass*my_map; (which I am not sure how many
byte it needs)
use more memory on stack, suppose the class is initialized as a stack
variable,
than "hash_map<strin g, MyClass**my_map ;" (which is just a pointer)?
Maybe, but not enough to matter.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 6 '07 #5

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

Similar topics

2
27911
by: dougjrs | last post by:
I have a HashMap that is storing form data that will later be inserted into a database. I have been able to create the HashMap just fine, but I wanted to be able to take my HashMap and just "dump" it out to the screen to make sure that everything is working as I expect. (It was really to easy to code so I think that I may be mising something). This is a piece of the code where I am updating the HashMap: if ( map.containsKey(temp)) {...
0
2105
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape. Once the compare is complete the file that was copied to a temp location needs to be deleted. I am using the method file.copy(sourcePath, tempPath, true) to copy the file and then file.delete(tempPath) to delete the file. On some of the files...
1
2590
by: Michael Jasn | last post by:
I had a question about memory management. Please look at the two functions below. Can you answer the two questions in the comments below. Thanks so much. -Mike func1(std::vector<Point>& points) { points.clear(); // Is this the right way to deallocate
4
10608
by: Dhans | last post by:
Hi there, I am using log4net in my (.net v1.1) application to log the messages. I tried to clear the logfile while application is running. since log4net has the file handle it did not allow me to clear/delete the file. I need to clear the content of the log file specified in the log4net configuration file, when it is needed in the application(such as some button click event), If anybody familiar with log4net your help would be...
11
2950
by: shiniskumar | last post by:
Ive got a collection with values Object= hashmap(k,v) Object= hashmap(k,v) Object= hashmap(k,v) Object= hashmap(k,v) Object= hashmap(k,v) i terated this collection and deleted object. then when i iterate again i get the following exception. java.util.ConcurrentModificationException
8
2217
by: psbasha | last post by:
Hi, I would like to clear /delete all the list data.How to procees without assigning empty data.Is there any method available?. Say: list1 = I dont want to do list1 =
5
7815
mickey0
by: mickey0 | last post by:
Hello, I have an hasmap with some words and their frequecy. Now, I need to delete the word with highest frequency. I'm new to Java and It's not clear to me how work/retrieve the content of a hash map; see this below. I thought to do this foreach to delete the word (but I don't like this way). Any suggestions? HashMap<String, Integer> text = new HashMap<String, Integer>(); int max = 0; String word = null; for (Integer freqOfWord :...
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10548
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
10316
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
6842
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
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
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
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.