The two Valgrind errors ("Mismatched free() / delete / delete []" and "18 bytes in 4 blocks are definitely lost") might be related.
In ~HashTable()
you call free(table[index2])
which probably means to destroy the UCSDStudent
objects (not sure, as you didn't post the whole program, esp. not the code which insert elements into HashTable
). I suppose you create UCSDStudent
objects with new
- and in that case, you also have to use the corresponding destruction method (in this case delete
instead of free()
). This is the cause for the first Valgrind error.
Furthermore, the free()
function will not call the object's destructor, while delete
will do that. This would explain why ~UCSDStudent()
is not called, causing your program to leak the memory for the student name. So using delete
instead of free()
in ~HashTable()
should solve both errors.
In general, you should try to stay with one way of memory allocation (either malloc()
/free()
or new/new[]/delete/delete[]
). And given that this is a C++ program, new
would be the appropriate choice. In the same vein, I'd advise you to remove the strdup()
and char*
stuff and switch to std::string
instead - this would remove another location where you might mix up free()
and delete
.