clear() before delete? --hash_map

N

newbie

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<string, MyClass*>);

....
....

for(hash_map said:
end(); ++it)
delete it->second;
// do I need call my_map->clear()?
delete my_map;

Thank,
 
M

Mark P

newbie said:
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".
 
R

red floyd

newbie said:
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<string, 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.
 
N

newbie

newbie said:
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<string, 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<string, MyClass*> *my_map;" (which is just a pointer)?

Thanks,
 
J

James Kanze

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<string, MyClass*> *my_map;" (which is just a pointer)?

Maybe, but not enough to matter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top