From 87d82ead963c24d84a4f6e417b96b9bf73d132bb Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Sun, 8 Jan 2023 14:44:25 -0800 Subject: fix memory problem --- src/hash_table.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/hash_table.c') diff --git a/src/hash_table.c b/src/hash_table.c index 6648bd1..29946b1 100644 --- a/src/hash_table.c +++ b/src/hash_table.c @@ -78,6 +78,15 @@ void sl_list_add(sl_list_t *l, char *key, ast_t *value) { } } +void sl_list_modify(sl_list_t *l, char *key, ast_t *value) { + sl_node_t *cur = l->head; + while (cur != NULL) { + if (strcmp(cur->value->key, key) == 0) + cur->value->value = value; + cur = cur->next; + } +} + ast_t *sl_list_get(sl_list_t *l, char *key) { sl_node_t *cur = l->head; for (int i = 0; i < l->size; i++) { @@ -123,6 +132,8 @@ hash_table_t *init_hash_table(int size) { void hash_table_add(hash_table_t *h, char *key, ast_t *value) { sl_list_t *l = h->buckets[hash(key, h->size)]; + if (sl_list_exists(l, key)) + sl_list_modify(l, key, value); sl_list_add(l, key, value); } -- cgit