i use this demo compile use fsanitize=address,leak, but it have some error.
include
include
include "khash.h"
KHASH_SET_INIT_STR(str)
int main(int argc, char argv[])
{
char s[4096]; // max string length: 4095 characters
khash_t(str) h;
khint_t k;
h = kh_init(str);
while (scanf("%s", s) > 0) {
int absent;
k = kh_put(str, h, s, &absent);
if (absent) kh_key(h, k) = strdup(s);
// else, the key is not touched; we do nothing
}
printf("# of distinct words: %d\n", kh_size(h));
// IMPORTANT: free memory allocated by strdup() above
for (k = 0; k < kh_end(h); ++k)
if (kh_exist(h, k))
free((char*)kh_key(h, k));
kh_destroy(str, h);
return 0;
}
and i debug found kh_destory free not malloc-ed memory cause error .anybody met this? thanks!
i use this demo compile use fsanitize=address,leak, but it have some error.
include
include
include "khash.h"
KHASH_SET_INIT_STR(str)
int main(int argc, char argv[]) { char s[4096]; // max string length: 4095 characters khash_t(str) h; khint_t k; h = kh_init(str); while (scanf("%s", s) > 0) { int absent; k = kh_put(str, h, s, &absent); if (absent) kh_key(h, k) = strdup(s); // else, the key is not touched; we do nothing } printf("# of distinct words: %d\n", kh_size(h)); // IMPORTANT: free memory allocated by strdup() above for (k = 0; k < kh_end(h); ++k) if (kh_exist(h, k)) free((char*)kh_key(h, k)); kh_destroy(str, h); return 0; }
and i debug found kh_destory free not malloc-ed memory cause error .anybody met this? thanks!