Collections-C icon indicating copy to clipboard operation
Collections-C copied to clipboard

Hashtable free keys

Open xfilesee opened this issue 8 years ago • 2 comments

  • In configuration a free function specifically to free keys in hashtable_remove and hashtable_destroy cases can be added. In current configuration the keys are left in heap if they were dynamically allocated.

xfilesee avatar Sep 28 '16 15:09 xfilesee

Hey, thanks for opening an issue :-)

If I understand you correctly, you're saying that there should be a pointer to a free function kept in the hashtable structure that would be used to automatically free data when it's removed from the table?

If that's the case, I think it is probably a good idea to add an option for that. Although I think it might be better to do it with a callback function instead of adding another field to the structure. You might want to take a look at this related issue.

srdja avatar Sep 28 '16 18:09 srdja

Thanks for the answer. I needed a solution immediately so I added 2 function pointers to both struct hashtable_s and hashtable_conf_s: table->key_free table->value_free By default NULL is assigned to both of these functions (to match the current configuration) and before calling I always check to make sure they are not NULL: like in hashtable_remove function: if(table->key_free != NULL) table->key_free(key);

xfilesee avatar Sep 29 '16 13:09 xfilesee