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.84k stars 326 forks source link

Hashtable free keys #65

Open xfilesee opened 8 years ago

xfilesee commented 8 years ago
srdja commented 8 years ago

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.

xfilesee commented 8 years ago

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);