srdja / Collections-C

A library of generic data structures for the C language.
http://srdja.github.io/Collections-C
GNU Lesser General Public License v3.0
2.8k stars 328 forks source link

Problem in hashtable #131

Closed jzijin closed 4 years ago

jzijin commented 4 years ago

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 of table->buckets is TableEntry**. Why do not just use table->buckets = conf->mem_calloc(table->capacity, sizeof(TableEntry*)); I modify table->buckets = conf->mem_calloc(table->capacity, sizeof(TableEntry)); to table->buckets = conf->mem_calloc(table->capacity, sizeof(TableEntry*)); and do test, It also run well!

alkeldi commented 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.