static - Using hashmap in C to store String - Integer mapping once and use for entire program run -
i have own implementation of c hash_map_t struct can use below?
// string value allocator allocator_t *str_value_allocator; allocator_init(&str_value_allocator, string_allocate_handler, string_deallocate_handler); str_hash_map_init(&str_hash_map, str_value_allocator, 5); str_hash_map_put(str_hash_map, test_key, test_val, strlen(test_val)); str_hash_map_get(str_hash_map, test_key, null) str_hash_map_remove(str_hash_map, test_key) str_hash_map_free(str_hash_map);
i use hash map in function below:
void handle_keyboard_input(char **tokens, size_t num_tokens) { char *virtual_key_name = strtok(tokens[1], " "); size_t num_flags = 0; char **modifier_flags = str_split(tokens[2], ", ", &num_flags); // map virtual_key_name (char *) virtual_key code (int) // foreach modifier flag (char *) map modifier flag code (int) }
i can create 2 hash_maps key_name -> key_code mapping , flag_name -> flag_code mapping. problem don't want create flag each time request handler function called have 1 data structure instance first call of function , in successive function invocations want reuse data structure (data store) created.
my hash_map created on heap there isn't possibility allocate array somewhere inside library source code file.
in java or c++ create singleton pattern or static member such concept not available in c language. create hash_map @ program startup somewhere @ beginning of program how pass reference library used program.
my recent idea use static hash_map_t variable inside handle_keyboard_input function
, somehow initialised when null (the first function call), , if variable isn't null in successive calls reuse initialised hash_map_t structure.
what best approach problem?
update
could use such code?
static str_hash_map_t *virtual_keys_map = null; static str_hash_map_t *modifier_flags_map = null; if (virtual_keys_map == null) { virtual_keys_map_init(&virtual_keys_map); } if (modifier_flags_map == null) { modifier_flags_map_init(&modifier_flags_map); }
since appears library, have several options:
you can make library more "object oriented" , force user proper instantiation. example, have adt struct defined
keyboardhandler
, ,handle_keyboard_input
instead:void kh_handle_input(keyboardhandler self, char **tokens, size_t num_tokens);
which means caller responsible doing instantiation of single part:
// caller must adt instance @ point, , don't care when keyboardhandler kh = kh_init(); kh_handle_input(kh, some_tokens, num_tokens); // other part can initialized later mousehandler mh = mh_init(); mh_handle_input(mh, some_tokens, num_tokens);
it's possible create library initializer both windows , posix dlls. can let done automatically instead.
otherwise, seems have make "check" anytime functions want use potentially-uninitialized hash tables (perhaps it's single function, anyway). in case, @ least refactor separate function:
void handle_keyboard_input(char **tokens, size_t num_tokens) { initialize_hashes_if_needed(); // ...and rest of function }
the reasoning don't want have modify several functions if decide there else needs malloced.
Comments
Post a Comment