Delete memory std :: map <int, string> completely

I have a card full and now I want to completely delete the memory. How do I do it right? could not find anything specific for this topic, sorry if he already answered ...

my code looks something like this:

for(std::map<short,std::string>::iterator ii=map.begin(); ii!=map.end(); ++ii) { delete &ii; } 

But that does not work. Can anyone help pls?

Regards, Phil

+4
source share
2 answers

The way to do it right is to not do it. A map will automatically release resources when it is destroyed for something allocated automatically.

If you did not select values ​​with new , you do not delete them.

 { std::map<short,std::string> x; x[0] = "str"; } //no leaks here { std::map<short,std::string*> x; x[0] = new std::string; delete x[0]; } 
+9
source

Just call map.clear(); . This will free all the objects that the card has allocated internally.

Note that in system tools such as task manager, your application can still display the same amount of memory. It is possible that the OS decides not to return the memory that your process once held in case it allocated it again. He will return it later if he begins to contract.

+5
source

All Articles