Question

I am programming my own implementation of a hash table. For this project I have an array of std::list which is my table where I store the data. Every list consists of std::pair objects. A pair contains a std::string (name of a person) and a pointer to an object of a custom class (class that contains data about the person).

I have a problem with the implementation of a put() method for inserting data in the hash table. This is the code I wrote.

pair<string,StudentRecord*>* HashTable::put(string& p_Name, StudentRecord* p_StudentRecord){


    std::pair<std::string, StudentRecord*> ptr = { p_Name, p_StudentRecord };
    this->put(&ptr);
    return &ptr;
}

void HashTable::put(pair<string, StudentRecord*>* p_HTElement){

    string key = p_HTElement->first;
    int storage_place = this->m_Hasher->hash(key) % this->m_Capacity;
    this->m_Table[storage_place].push_back(p_HTElement);
    this->m_NumberOfEntries++;
    this->updateLoadFactor();

    if (this->m_LoadFactor >= MAX_LOAD_FACTOR)
        this->rehash();
}

When data needs to be added the first method is called. This method creates a std::pair object and passes a reference to this object to the second method. The second method then calculates the hash and places it in the array of std::list. However the problem is that after putting it in the array the std::string (the first element of the pair) isn't readable anymore. When I look with the debugger it just says its value is "". When in a later stage I want to find the data in the hash table my method printHashTable() does recognises their is a pair in the list, but it can't read the data. Again with the debugger it says

error reading characters of string

for the first element of the pair and for the custom object it says 0xccccccccc

This is my method which needs to print all the data in the hash table:

void HashTable::printTable(){
    for (int i = 0; i < this->m_Capacity; i++){
        if (!this->m_Table[i].empty()) {
            for (std::list<std::pair<std::string, StudentRecord*>*>::iterator element = this->m_Table[i].begin(); element != this->m_Table[i].end(); ++element) {
                cout << (*element)->first << endl;
            }
        }
    }
}
Was it helpful?

Solution

You store a pointer to the reference of an object. But that object gets dereferenced when you leave your first function, so you table points to uninitialized memory. I would advise not to store data with pointers, but use emplace methods to avoid making copies.

std::list<std::pair<std::string, StudentRecord*>*> does not own the objects it points to, just the pointer.

You have to make a copy, you can enforce this by changing the type to:

std::list<std::pair<std::string, StudentRecord*>>

You will have to adapt your m_Table type as well, from your example I cannot see what it is.

If you want to keep the pointer then, you need to change

std::pair<std::string, StudentRecord*> ptr = { p_Name, p_StudentRecord };

to allocate the object on the heap, instead of the stack. (i.e. use new)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top