Why do you have a global variable for your hash table? Instead, you should probably create a structure or class, which can contain the size of the table and a pointer to the table, and allocate its memory dynamically. The following has a default size, but you can pass in a different size when creating the hash table to try out different sizes.
class HashTable {
public:
HashTable(int size = 1010081) : m_size(size) {
m_table = new nlist *[m_size];
}
~HashTable() {
delete[] m_table;
}
// Define getters, setters, etc. here...
private:
int m_size;
struct nlist **m_table;
};
note: I'm assuming (based on the fact that you're trying to implement your own hash table, and some of your previous questions) that you are interested in learning about the low-level implementation of a hash table, and thus I'm giving you a fairly low-level answer about how to allocate and deallocate the memory yourself. In a real-world program, using std::vector
, as described by several of the other answers, would probably be the right thing to do, as it reduces the amount of bookkeeping you need to do yourself. Then again, in a real-world program, you probably wouldn't want to implement your own hash table, but instead use an existing has table implementation like hash_map
(not standard, but widely available), boost::unordered_map
, or std::tr1::unordered_map
(this is on the track to becoming a standard, and based on boost::unordered_map
).