Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

old_val pointer in README example not initialized. #19

Open
vassvik opened this issue Aug 16, 2018 · 0 comments
Open

old_val pointer in README example not initialized. #19

vassvik opened this issue Aug 16, 2018 · 0 comments

Comments

@vassvik
Copy link

vassvik commented Aug 16, 2018

The old_val pointer in the README example isn't initialized. This results in the last conditional falsely firing on some compilers.

charchar *key = strdup( *key = st "my key");
char *val = strdup("my val");

// if a caller knows the value doesn't already exist
// BEWARE: using it this way can cause memory leaks!
if (!rsht_put(ht, key, val, NULL)) {
  // handle out of memory error
}

// if a caller wants to know the previous value and free it
void *old_val; // Should be initialized to zero
if (!rsht_put(ht, key, val, &old_val)) {
  // handle out of memory error
  free(key);
}
if (old_val) {
  free(key); // if the value existed, we can free the key we just used
  free(old_val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant