Reputation: 127
Consider
map <char,node*> mp;
where node is a class
If we use mp.clear();
then is it required to free the memory explicitly?
Upvotes: 2
Views: 254
Reputation: 71999
The problem here is that a raw pointer conveys no information about ownership, i.e. who is responsible for cleaning up the nodes. Is this map the owner of the nodes? Then you need to delete the objects. Or far better, you use unique_ptr
instead of raw pointers. But if the map is not the owner of the nodes, then you must not delete the nodes.
The bottom line is to understand the concept of ownership and use an appropriate smart pointer.
Upvotes: 4