HashMap Memory Leak because of Dynamic Array
- by Jake M
I am attempting to create my own HashMap to understand how they work. I am using an array of Linked Lists to store the values(strings) in my hashmap.
I am creating the array like this:
Node** list;
Instead of this:
Node* list[nSize];
This is so the array can be any size at runtime. But I think I am getting a memory leak because of how I am doing this. I dont know where the error is but when I run the following simple code the .exe crashes.
Why is my application crashing and how can I fix it?
Note: I am aware that using a vector would be much better than an array but this is just for learning and I want to challenge myself to create the hashmap using a 'Dynamic' Array. PS: is that the correct term(Dynamic Array) for the kind of array I am using?
struct Node
{
// to implement
};
class HashMap
{
public:
HashMap(int dynSize)
{
*list = new Node[dynSize];
size = dynSize;
for (int i=0; i<size; i++)
list[i] = NULL;
cout << "END\n";
}
~HashMap()
{
for (int i=0; i<size; i++)
delete list[i];
}
private:
Node** list; // I could use a vector here but I am experimenting with a pointer to an array(pointer), also its more elegant
int size;
};
int main()
{
// When I run this application it crashes. Where is my memory leak?
HashMap h(5);
system("PAUSE");
return 0;
}