hashmap icon indicating copy to clipboard operation
hashmap copied to clipboard

incovenient function type in hashmap_set_key_alloc_funcs

Open pseregiet opened this issue 2 years ago • 1 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 *) ?

pseregiet avatar Jun 11 '23 11:06 pseregiet

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

dleeds-cpi avatar Nov 06 '23 06:11 dleeds-cpi