Author has written ht_del_hash_table function as below.
void ht_del_hash_table(ht_hash_table* ht) {
for (int i = 0; i < ht->size; i++) {
ht_item* item = ht->items[i];
if (item != NULL) {
ht_del_item(item);
}
}
free(ht->items);
free(ht);
}
But there is a problem, when we later add function to delete an item from the hash table, we do it via making the pointer equal to HT_DELETED_ITEM.
So if we delete an item from hash table and then after delete the whole table then it will be segment fault. Becuase above function only checks for NULL, the item pointing to HT_DELETED_ITEM will be passed to ht_del_item function. That function is as following.
static void ht_del_item(ht_item* i) {
free(i->key);
free(i->value);
free(i);
}
this will try to free the HT_DELETED_ITEM and result in segment fault.
I think we should add the check for HT_DELETED_ITEM too.
Correct function should be :
void ht_del_hash_table(ht_hash_table *ht) {
for (int i = 0; i < ht->size; ++i) {
ht_item* item = ht->items[i];
if (item != NULL && item != &HT_DELETED_ITEM) {
ht_del_item(item);
}
}
free(ht->items);
free(ht);
}
Author has written ht_del_hash_table function as below.
But there is a problem, when we later add function to delete an item from the hash table, we do it via making the pointer equal to HT_DELETED_ITEM.
So if we delete an item from hash table and then after delete the whole table then it will be segment fault. Becuase above function only checks for NULL, the item pointing to HT_DELETED_ITEM will be passed to ht_del_item function. That function is as following.
this will try to free the HT_DELETED_ITEM and result in segment fault.
I think we should add the check for HT_DELETED_ITEM too.
Correct function should be :