Skip to content

Commit

Permalink
fix(codes/c): Bug fixes in discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonglja committed Nov 26, 2023
1 parent 0365bc7 commit 3b9c838
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion codes/c/chapter_hashing/array_hash_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void pairSet(ArrayHashMap *hmap, MapSet *set) {
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
entries[index].key = hmap->buckets[i]->key;
entries[index].val = malloc(strlen(hmap->buckets[i]->val + 1));
entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);
strcpy(entries[index].val, hmap->buckets[i]->val);
index++;
}
Expand Down
10 changes: 8 additions & 2 deletions codes/c/chapter_sorting/counting_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void countingSortNaive(int nums[], int size) {
}
// 2. 统计各数字的出现次数
// counter[num] 代表 num 的出现次数
int *counter = malloc(sizeof(int) * m);
int *counter = calloc(sizeof(int) * m);
for (int i = 0; i < size; i++) {
counter[nums[i]]++;
}
Expand All @@ -29,6 +29,9 @@ void countingSortNaive(int nums[], int size) {
nums[i] = num;
}
}

// 4. 释放内存
free(counter);
}

/* 计数排序 */
Expand All @@ -43,7 +46,7 @@ void countingSort(int nums[], int size) {
}
// 2. 统计各数字的出现次数
// counter[num] 代表 num 的出现次数
int *counter = malloc(sizeof(int) * m);
int *counter = calloc(sizeof(int) * m);
for (int i = 0; i < size; i++) {
counter[nums[i]]++;
}
Expand All @@ -62,6 +65,9 @@ void countingSort(int nums[], int size) {
}
// 使用结果数组 res 覆盖原数组 nums
memcpy(nums, res, size * sizeof(int));

// 5. 释放内存
free(counter);
}

/* Driver Code */
Expand Down

0 comments on commit 3b9c838

Please sign in to comment.