I'm building a hashmap class that can have string keys and ints, bools, strings or pointers of different types as its values, and I want it to work. For the program I'm using it for I create the pointer and pass it into the hashmap. The problem comes when I need to destruct the map. If the type for the hashmap is a pointer I need to delete it(the value) before I delete it's container.
so the code I have right now goes something like this: I have a hashNode** drawers, which I use as a two dimensional array to hold pointer to hashNodes in the map. Those same pointers are also held in another hashNode** array, which stores them as they are added to map (for ease/speed of growing and copying the hashmap).
开发者_开发百科template <typename V>
class str_map {
public:
// ...
virtual ~str_map() {
str_map<V>::~str_map();
}
// ....
};
and then later I have a bunch of methods like these: one for regular values:
template <>
str_map<int>::~str_map() {
for(int i=0; i < count && array[i] != NULL; i++){
delete array[i];
}
delete array;
delete drawers;
}
and one for pointers:
template <>
str_map<str_map<int>*>::~str_map() {
for(int i=0; i < count && array[i]->val() != NULL; i++)
delete array[i]->val();
for(int i=0; i < count && array[i] != NULL; i++){
delete array[i];
}
delete array;
delete drawers;
}
Is there another better way to deconstruct an instance of str_map class correctly so that all the memory is handled correctly? Or at least a way to make this work?
Your container should handle values. That's it, no more, no less. If someone wants to stick pointers in, let them. Don't take ownership of whatever they may or may not be pointing at.
It's up to the users of your hash map to know how to manage the lifetime of their memory. They should be using smart pointers, so your class just copies them around and the smart pointer manages the memory.
The guideline is manage one resource, or none at all. If you are managing more than one resource, you've set yourself up for failure.
I suspect delete array
should be delete [] array;
. What this means is you really should be using std::vector
. Again, either manage one resource or none at all. std::vector
manages one resource, so you don't have to. And so on.
精彩评论