Why do you have a global variable for your hash table? Instead, you should probably create a structure or class that can contain the size of the table and a pointer to the table, and dynamically allocate its memory. The next size has a default size, but when creating a hash table, you can pass different sizes.
class HashTable { public: HashTable(int size = 1010081) : m_size(size) { m_table = new nlist *[m_size]; } ~HashTable() { delete[] m_table; }
note: I assume (assuming that you are trying to implement your own hash table and some of your previous questions) that you are interested in learning about the implementation of the hash table at a low level, and therefore I give you a rather low level of answer about how to allocate and free memory on your own. In a real program, using std::vector as described in several other answers would probably be the right decision, as this reduces the amount of accounting you need to do yourself. Again, in a real program, you probably wonβt want to implement your own hash table, but instead use an existing table implementation, for example hash_map (not standard, but widely available), boost::unordered_map , or std::tr1::unordered_map (this becomes a standard on the track and is based on boost::unordered_map ).
source share