Reputation: 1835
I am working with a Hashtable struct that maps keys to values, where the values are (void *) so that Hashtables can hold any kind of value.
To be able to free those values, the deconstructor of a Hashtable takes in a pointer to a freeing function as an argument. In my case, I know I am going to be freeing basic types, like char* and int*. Is it possible to pass in a pointer to the free() function, since this can deal with basic types?
Something like this:
FreeHashTable(hashtable_name, free);
Upvotes: 1
Views: 193
Reputation: 5456
You can (and should) pass to free
every pointer that has returned by malloc
, no matter which type (or struct) it points to. Be careful to not pass to free
pointers that you didn't get from malloc. (Middle of arrays, local variables, etc)
BTW, unless some of your data types need some work before freeing, you can do it without pass pointer to function - just call free
.
Upvotes: 2
Reputation: 258688
You don't need to pass the pointer to the function.
Just loop through the values and call free
.
Upvotes: 0