From db164f670335fd5d88cd37d058fa0357279abf63 Mon Sep 17 00:00:00 2001 From: Yang Le Date: Sun, 7 Apr 2024 01:36:55 +0800 Subject: [PATCH] fix memory leak in the C code (#1203) * fix memory leak when size(deque) == 1 * fix memory leak --- codes/c/chapter_hashing/hash_map_open_addressing.c | 3 +++ codes/c/chapter_stack_and_queue/linkedlist_deque.c | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/codes/c/chapter_hashing/hash_map_open_addressing.c b/codes/c/chapter_hashing/hash_map_open_addressing.c index ffcb2d980..55dadd85d 100644 --- a/codes/c/chapter_hashing/hash_map_open_addressing.c +++ b/codes/c/chapter_hashing/hash_map_open_addressing.c @@ -49,6 +49,9 @@ void delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) { free(pair); } } + free(hashMap->buckets); + free(hashMap->TOMBSTONE); + free(hashMap); } /* 哈希函数 */ diff --git a/codes/c/chapter_stack_and_queue/linkedlist_deque.c b/codes/c/chapter_stack_and_queue/linkedlist_deque.c index 53569ec6f..f28671683 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_deque.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_deque.c @@ -122,8 +122,8 @@ int pop(LinkedListDeque *deque, bool isFront) { if (fNext) { fNext->prev = NULL; deque->front->next = NULL; - delDoublyListNode(deque->front); } + delDoublyListNode(deque->front); deque->front = fNext; // 更新头节点 } // 队尾出队操作 @@ -133,8 +133,8 @@ int pop(LinkedListDeque *deque, bool isFront) { if (rPrev) { rPrev->next = NULL; deque->rear->prev = NULL; - delDoublyListNode(deque->rear); } + delDoublyListNode(deque->rear); deque->rear = rPrev; // 更新尾节点 } deque->queSize--; // 更新队列长度