I was stepping through a piece of code for hash map implementation. I did not understand an implementation where the 2 dimensional hash table is assigned memory. Can someone explain what this piece of code does?
table = new LinkedHashEntry* [TABLE_SIZE];
Attaching some lines of code before it for more clarity
1 2 3 4 5 6 7 8
private:
LinkedHashEntry **table;
public:
HashMap() {
table = new LinkedHashEntry* [TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = NULL;
}
I think it will assign memory for LinkedHashEntry pointers for table size. Assuming TABLE_SIZE is 128 here, table will point to 128 LinkedHashEntries. Is that correct?
It dynamically allocates an array of pointers - the size of the array of pointers is TABLE_SIZE. Since that's in all-caps, I assume it is a constant, which means that dynamically allocating it is a waste of effort.
Hi LB,
Thanks for your reply. You are correct, the TABLE_SIZE value is a constant, but table data structure is the one which will be assigned size dynamically. So can you please tell me why you think it is waste of effort to do something like this