Skip to content

Commit

Permalink
fix(c): Fix malloc allocation of secondary pointers can lead to dump …
Browse files Browse the repository at this point in the history
…issues (#1367)
  • Loading branch information
Gonglja committed May 18, 2024
1 parent 9afbc9e commit 0e22154
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions codes/c/chapter_hashing/hash_map_open_addressing.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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++) {
Expand Down
4 changes: 2 additions & 2 deletions zh-hant/codes/c/chapter_hashing/hash_map_open_addressing.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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++) {
Expand Down

0 comments on commit 0e22154

Please sign in to comment.