c-hashmap icon indicating copy to clipboard operation
c-hashmap copied to clipboard

"Proper Usage of Keys" doc clarification

Open u-an-i opened this issue 1 year ago • 0 comments

I had trouble understanding

  • a different key variable of equal content can be used to look up (get) an entry set using another key variable (but same string content)
  • a value update (set) can use a different key variable equally, in that case only the value gets updated, the key does not get replaced by the different variables key of equal content.

So the key really is the string and not the pointer value. You mentioning strlen being O(n) unsuitable for performance I wondered whether you don't use the string content but the pointer value only.

However, emphasizing a local function string (char s[]) cannot be used for initial set keys might be useful, since that string is "destroyed" on function exit, a malloced char* s is necessary (which can be freed by iterating like you mentioned).

Rewording your doc, start from my last point:

  • initially use a malloced char* or a program-permanent string (C-Strings used directly are referenced from the program data section I believe) as key, don't worry about losing it, use iterate to free malloced char* keys before program exit (but don't mix malloced char* and prog-perm strings in the same hashmap if you cannot distinguish them in iterate)
  • any lookup and update may use a different even temporary variable containing an equal string.

u-an-i avatar Oct 02 '23 11:10 u-an-i