mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 03:26:27 +08:00
fix(c): Fix malloc allocation of secondary pointers can lead to dump issues (#1367)
This commit is contained in:
parent
9afbc9eda5
commit
0e221540a3
2 changed files with 4 additions and 4 deletions
|
@ -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++) {
|
||||
|
|
|
@ -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++) {
|
||||
|
|
Loading…
Reference in a new issue