I am coding a HashTable using a Doubly Linked list for one of my classes, but I am running into an error when I run valgrind. It says: Conditional jump or move depends on uninitialised value(s). When I run --track-origins=yes, it says:
==11453== Conditional jump or move depends on uninitialised value(s)
==11453== at 0x402904: LinkedList<std::string>::itemExists(std::string const&) (LinkedList.h:89)
==11453== by 0x401FE6: HashSet<std::string>::find(std::string const&) (HashSet.h:85)
==11453== by 0x401E42: HashSet<std::string>::add(std::string const&) (HashSet.h:39)
==11453== by 0x4019D7: insert(std::string, std::basic_ifstream<char, std::char_traits<char> >&, std::basic_ofstream<char, std::char_traits<char> >&, std::string, HashSet<std::string>&) (main.cpp:64)
==11453== by 0x4015B1: main (main.cpp:34)
==11453== Uninitialised value was created by a heap allocation
==11453== at 0x4C2B800: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11453== by 0x4024D8: HashSet<std::string>::rehash() (HashSet.h:131)
==11453== by 0x401E69: HashSet<std::string>::add(std::string const&) (HashSet.h:45)
==11453== by 0x4019D7: insert(std::string, std::basic_ifstream<char, std::char_traits<char> >&, std::basic_ofstream<char, std::char_traits<char> >&, std::string, HashSet<std::string>&) (main.cpp:64)
==11453== by 0x4015B1: main (main.cpp:34)
So I think the problem is in my rehash function, but I can't figure out where it is. Could you guys help me out? Here is my rehash function:
void rehash()
{
int old_size = tableSize;
if (tableSize == itemsStored) {
tableSize = tableSize * 2 + 1;
}
else if (itemsStored <= tableSize/2) {
tableSize = tableSize / 2;
}
LinkedList<string>* newTable = new LinkedList<string>[tableSize]; -------> line 131
for (int i = 0; i < old_size; ++i)
{
int table_size = 0;
table_size = table[i].getSize();
if(table_size != 0) {
for (int j = 0; j < table_size; ++j) {
unsigned index = hashFunction(table[i].getItem(j)) % tableSize;
newTable[index].insert(newTable[index].getSize(), table->getItem(i));
}
}
}
LinkedList<string>* temp = table;
table = newTable;
delete [] temp;
}
And this is my constructor for the Hash Set:
HashSet()
{
int tableSize = 0;
table = new LinkedList<string>[tableSize];
}
This is my constructor for the LinkedList:
LinkedList()
{
size = 0;
}
Thank you!
insertcode-path. Why don't you step through with a debugger to see what's going on? - Oliver Charlesworth