mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-06 05:48:58 +08:00
Add space_complexit under C and fix memory leak under CPP (#456)
* fix(codes/cpp): Memory leak fix: the space was not freed when pop removed the element. * fix(codes/cpp): Fix access error when printArray(arr, 0) * fix(codes/cpp): Fix memory leaks: replace pointers with local variables, no need to manage memory * fix(codes/cpp): Fix memory leaks: no delete * fix(codes/cpp): Fix memory leaks: Add destructor ~ArrayHashMap() * Update PrintUtil.hpp * feat(codes/c): Add three-party hash implementation * feat(codes/c): Add freeMemoryTree in tree_node.h * feat(codes/c): Add space_complexity.c * styles(codes/c): Modify format * feat(codes/cpp): Undo a previous delete, there is no memory leak here * Update array_hash_map.cpp * Update include.h --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@ -28,6 +28,14 @@ class ArrayHashMap {
|
||||
buckets = vector<Entry *>(100);
|
||||
}
|
||||
|
||||
~ArrayHashMap() {
|
||||
// 释放内存
|
||||
for (const auto &bucket : buckets) {
|
||||
delete bucket;
|
||||
}
|
||||
buckets.clear();
|
||||
}
|
||||
|
||||
/* 哈希函数 */
|
||||
int hashFunc(int key) {
|
||||
int index = key % 100;
|
||||
@ -53,7 +61,8 @@ class ArrayHashMap {
|
||||
/* 删除操作 */
|
||||
void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
// 置为 nullptr ,代表删除
|
||||
// 释放内存并置为 nullptr
|
||||
delete buckets[index];
|
||||
buckets[index] = nullptr;
|
||||
}
|
||||
|
||||
@ -141,4 +150,4 @@ int main() {
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user