0

I am trying to make a basic HashMap. I am checking to see if an element exists at an index before inserting it there. When I insert my first element, it says that an element already exists at that position. I have gone through the debugger, and all of my values are as expected, except for map[hash]. I am anticipating a nullptr, but it is not coming. map[hash] has the following value:

-       map[hash]   0xcdcdcdcd {key=??? value={...} next_element=??? }  HashElement *

Can someone please explain to me what I am misunderstanding here? The unexpected result is on line 21 of HashMap.cpp. Here is the relevant code:

HashMap.h

#pragma once
#include <string>

#include "HashElement.h"

class HashMap
{
private:
    HashElement **map;
    int size;
public:
    HashMap(int);
    ~HashMap();
    int GetHash(int);
    void Put(int, std::string);
};

HashMap.cpp

#include "HashMap.h"

#include <string>

HashMap::HashMap(int _size)
{
    size = _size;
    map = new HashElement*[size];
}

HashMap::~HashMap()
{
}

int HashMap::GetHash(int _key){
    return _key % size;
}

void HashMap::Put(int _key, std::string _value){
    int hash = GetHash(_key);
    if (!map[hash]){  //Anticipated to be nullptr on first Put, but it skips to else
        map[hash] = new HashElement(_key, _value);
    }
    else{
        HashElement *lastElement = map[hash];
        while (lastElement->next_element){
            lastElement = lastElement->next_element;
        }
        lastElement->next_element = new HashElement(_key, _value);
    }
}

HashElement.h

#pragma once

#include <string>

class HashElement
{
private:
    int key;
    std::string value;
public:
    HashElement(int, std::string);
    ~HashElement();
    HashElement *next_element;
    int get_key();
    std::string get_value();
};

HashElement.cpp

#include "HashElement.h"

HashElement::HashElement(int _key, std::string _value)
{
    key = _key;
    value = _value;
}

HashElement::~HashElement()
{
}

int HashElement::get_key(){
    return key;
}

std::string HashElement::get_value(){
    return value;
}
Evorlor
  • 7,263
  • 17
  • 70
  • 141

2 Answers2

1

map[hash] is not a nullptr because you haven't initialized it to such.

map = new HashElement*[size];

Each element in the map array will have a random value after that line.

To fix this and initialize all elements to be nullptr:

map = new HashElement*[size]();
                            ^^
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • I think you may be stalking me...following me around and answering all of my questions...dont stop :-) – Evorlor Mar 01 '15 at 17:47
1
map = new HashElement*[size];

Here, you are instantiating an array of size pointers, on the heap. As I understand your question, you are assuming that all of the instantiated pointers, in this new array, will be nullptr.

That is not the case. For "plain old data", or POD, its contents are not initialized by default. You'll have to explicitly initialize them:

for (size_t i=0; i<size; ++i)
    map[i]=0;

... in the constructor

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148