HashMap Memory Leak because of Dynamic Array

Posted by Jake M on Stack Overflow See other posts from Stack Overflow or by Jake M
Published on 2012-12-15T04:19:11Z Indexed on 2012/12/15 5:04 UTC
Read the original article Hit count: 176

Filed under:
|

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;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about memory-leaks