DavidLeeds / hashmap

Templated type-safe hashmap implementation in C using open addressing and linear probing for collision resolution.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

incovenient function type in hashmap_set_key_alloc_funcs

pseregiet opened this issue · comments

Because of the type of the free function this takes the most common use case of just passing 'free' produces a warning

Incompatible function pointer types initializing 'typeof (((&skined_anim_mngr.map))->map_types->t_key_free_func)' (aka 'void (*)(char *)') with an expression of type 'void (void *)'

Therefore wouldn't it make sense to change the type to void free(char *) ?

Hi @pseregiet we could certainly accomplish what you are asking, but it would be at the cost of type safety. All the macros in hashmap.h perform a cast or assignment in order to allow the compiler to type check. While it is likely that free() will be used, we cannot presume that it will always be used. Thus, we have the choice of forcing the key free function to be void (*)(void *) (potentially breaking code that uses void (*)(key_type *)), or we accept any function pointer and cast it, which may result in crashes if an incompatible pointer is passed in. Neither sound particularly appealing.

My recommendation, would be to define a trivial function to wrap free() for your key type. E.g.

void free_key(char *key)
{
    free(key);
}