Adam
Adam

Reputation: 65

C++ Hash Table using chaining, remove method

I'm implementing a Hash Table in C++ using chaining. The code builds with no errors and the table constucts fine using the insert method. However, when I call the remove method I receive the following error:

Unhandled exception at 0x00c53be9 in HashTable.exe: 0xC0000005: Access violation reading location 0x00000000.

Hash Entry Code:

#include <string>
#include <vector>

template <class T>
class HashEntry
{
private:
    int key; //lookup key
    T value; //hash data
    HashEntry<T> *next;

public:
    HashEntry(int key, T value);
    HashEntry();
    int& getKey();
    T& getValue();
    void setValue(T value);
    HashEntry<T>* getNext();
    void setNext(HashEntry *next);
    bool operator == (HashEntry& rhs);
    bool operator != (HashEntry& rhs);
    HashEntry<T>& operator = (HashEntry& rhs);
};

template <class T> 
HashEntry<T>::HashEntry(int key, T value)
{
    this->key = key;
    this->value = value;
    this->next= nullptr;
}

template <class T>
HashEntry<T>::HashEntry()
{
    this->key = 0;
    this->next= nullptr;
}

template <class T>
int& HashEntry<T>::getKey()
{
    return key;
}

template <class T>
T& HashEntry<T>::getValue()
{
    return value;
}

template <class T>
void HashEntry<T>::setValue(T value)
{
    this->value = value;
}

template <class T>
HashEntry<T>* HashEntry<T>::getNext()
{
    return next;
}

template <class T>
void HashEntry<T>::setNext (HashEntry *next)
{
    this->next = next;
}

template <class T>
bool HashEntry<T>::operator == (HashEntry& rhs)
{
    return ((this->getKey() == rhs.getKey()) && (this->getValue() == rhs.getValue()));
}

template <class T>
bool HashEntry<T>::operator != (HashEntry& rhs)
{
    return ((this->getKey() != rhs.getKey()) && (this->getValue() != rhs.getValue()));
}

template <class T>
HashEntry<T>& HashEntry<T>::operator = (HashEntry& rhs)
{
    this->key = rhs.getKey();
    this->value = rhs.getValue();
    this->next = rhs.getNext();

    return *this;
}

Hash Table code:

template <class T>
class HashTable
{
private:
    std::vector<HashEntry<T>> table;
    static const int DEFAULT_TABLE_SIZE = 128;
    int TABLE_SIZE;


public:

    HashTable();
    void insert(int key, T value);
    void remove(int key);
    void get(int key);
    ~HashTable();
};

template <class T>
HashTable<T>::HashTable()
{
    TABLE_SIZE = DEFAULT_TABLE_SIZE;
    table.resize(TABLE_SIZE);
}

Remove Method Code:

template <class T>
void HashTable<T>::remove(int key)
{
    int hashFunc = (key % TABLE_SIZE);

    if (table[hashFunc] != HashEntry<T>())
    {
        HashEntry<T> prevEntry = HashEntry<T>();
        HashEntry<T> entry = table[hashFunc];
        while (entry.getNext() != nullptr && entry.getKey() != key)
        {
            prevEntry = entry;
            entry = *entry.getNext();
        }
        if (entry.getKey() == key)
        {
            if (prevEntry == HashEntry<T>())
            {
                HashEntry<T> nextEntry = *entry.getNext(); //Where the exception is thrown
                entry = HashEntry<T>();
                table[hashFunc] = nextEntry;
            }

            else
            {
                HashEntry<T> *next = entry.getNext();
                entry = HashEntry<T>();
                prevEntry.setNext(next);
            }
        }
    }
}

Upvotes: 2

Views: 5775

Answers (1)

Griwes
Griwes

Reputation: 9039

while (entry.getNext() != nullptr && entry.getKey() != key)
{
    prevEntry = entry;
    entry = *entry.getNext();
}

Few lines later, using entry generated above:

HashEntry<T> nextEntry = *entry.getNext(); //Where the exception is thrown

The while "makes" entry.getNext() a nullptr. And, later, you are trying to dereference it. And dereferencing nullptr... is a Bad Thing (tm).

Btw., why aren't you operating on pointers? I may be wrong, but looking at your code, I have a feeling that you want to modify original objects... and local objects looks like copies.

Upvotes: 1

Related Questions