From 0e221540a3b4a410b2ea3950558e290c4053d54f Mon Sep 17 00:00:00 2001 From: Lanjing Gong <39959756+Gonglja@users.noreply.github.com> Date: Sat, 18 May 2024 18:17:33 +0800 Subject: [PATCH] fix(c): Fix malloc allocation of secondary pointers can lead to dump issues (#1367) --- codes/c/chapter_hashing/hash_map_open_addressing.c | 4 ++-- zh-hant/codes/c/chapter_hashing/hash_map_open_addressing.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/codes/c/chapter_hashing/hash_map_open_addressing.c b/codes/c/chapter_hashing/hash_map_open_addressing.c index 55dadd85d..e4e00fb08 100644 --- a/codes/c/chapter_hashing/hash_map_open_addressing.c +++ b/codes/c/chapter_hashing/hash_map_open_addressing.c @@ -32,7 +32,7 @@ HashMapOpenAddressing *newHashMapOpenAddressing() { hashMap->capacity = 4; hashMap->loadThres = 2.0 / 3.0; hashMap->extendRatio = 2; - hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity); + hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *)); hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair)); hashMap->TOMBSTONE->key = -1; hashMap->TOMBSTONE->val = "-1"; @@ -151,7 +151,7 @@ void extend(HashMapOpenAddressing *hashMap) { int oldCapacity = hashMap->capacity; // 初始化扩容后的新哈希表 hashMap->capacity *= hashMap->extendRatio; - hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity); + hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *)); hashMap->size = 0; // 将键值对从原哈希表搬运至新哈希表 for (int i = 0; i < oldCapacity; i++) { diff --git a/zh-hant/codes/c/chapter_hashing/hash_map_open_addressing.c b/zh-hant/codes/c/chapter_hashing/hash_map_open_addressing.c index 41908868d..1df03417e 100644 --- a/zh-hant/codes/c/chapter_hashing/hash_map_open_addressing.c +++ b/zh-hant/codes/c/chapter_hashing/hash_map_open_addressing.c @@ -32,7 +32,7 @@ HashMapOpenAddressing *newHashMapOpenAddressing() { hashMap->capacity = 4; hashMap->loadThres = 2.0 / 3.0; hashMap->extendRatio = 2; - hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity); + hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *)); hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair)); hashMap->TOMBSTONE->key = -1; hashMap->TOMBSTONE->val = "-1"; @@ -151,7 +151,7 @@ void extend(HashMapOpenAddressing *hashMap) { int oldCapacity = hashMap->capacity; // 初始化擴容後的新雜湊表 hashMap->capacity *= hashMap->extendRatio; - hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity); + hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *)); hashMap->size = 0; // 將鍵值對從原雜湊表搬運至新雜湊表 for (int i = 0; i < oldCapacity; i++) {