Closed jzijin closed 4 years ago
I think you are correct. When allocating the buckets memory, only the size of a pointer is needed. The memory of the TableEntry struct itself is allocated separately in hashtable_add
. The reason it works now is because the allocated memory is more than what is actually needed, but this is wasting too much memory.
The function hashtable_new_conf in hashtable.c. I found that
table->buckets = conf->mem_calloc(table->capacity, sizeof(TableEntry));
It confuse me. The type oftable->buckets
isTableEntry**
. Why do not just usetable->buckets = conf->mem_calloc(table->capacity, sizeof(TableEntry*));
I modifytable->buckets = conf->mem_calloc(table->capacity, sizeof(TableEntry));
totable->buckets = conf->mem_calloc(table->capacity, sizeof(TableEntry*));
and do test, It also run well!