Closed 7PintsOfCherryGarcia closed 2 years ago
The right way to test itr == kh_end(map)
. kh_exist()
is only used when you iterate a hash table. I know the naming is confusing. Sorry for that.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "klib/khashl.h"
KHASHL_MAP_INIT(static,
intmap_t, intmap,
uint64_t, uint32_t,
kh_hash_uint64,
kh_eq_generic)
int main()
{
intmap_t *map = intmap_init();
khint_t itr;
int absent;
for (uint64_t i = 0; i < 100; i++) {
itr = intmap_put(map, i, &absent);
kh_val(map, itr) = (uint32_t)(i+1);
}
fprintf(stderr, "size: %u\n", kh_size(map));
for (uint64_t i = 95; i < 105; i++) {
itr = intmap_get(map, i);
fprintf(stderr, "%lu exits? %s\n", i , itr == kh_end(map) ? "yes" : "no");
}
intmap_destroy(map);
}
Valgrind is throwing an "invalid read" when querying if an element not in a hash is present in a hash table.
The following code produces the error:
The error:
I suspect the error resides in ##_getp but not sure how to fix it, I will keep giving it a try.