Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C代码的哈希表部分运行时出现了段错误 #1237

Closed
AuroraOlian opened this issue Apr 7, 2024 · 1 comment
Closed

C代码的哈希表部分运行时出现了段错误 #1237

AuroraOlian opened this issue Apr 7, 2024 · 1 comment

Comments

@AuroraOlian
Copy link

AuroraOlian commented Apr 7, 2024

c/chapter_hashing/array_hash_map.c中, 构造函数并未初始化哈希表中的桶,运行时出现了段错误,建议做出如下修改:

/* 构造函数(修改前) */
ArrayHashMap *newArrayHashMap() {
    ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));
    return hmap;
}
/* 构造函数(修改后) */
ArrayHashMap *newArrayHashMap() {
    ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));
    for (int i = 0; i < HASHTABLE_CAPACITY; i++) {
        hmap->buckets[i] = NULL;
    }
    return hmap;
}
Gonglja added a commit to Gonglja/hello-algo that referenced this issue Apr 11, 2024
@Gonglja
Copy link
Contributor

Gonglja commented Apr 11, 2024

c/chapter_hashing/array_hash_map.c中, 构造函数并未初始化哈希表中的桶,运行时出现了段错误,建议做出如下修改:

/* 构造函数(修改前) */
ArrayHashMap *newArrayHashMap() {
    ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));
    return hmap;
}
/* 构造函数(修改后) */
ArrayHashMap *newArrayHashMap() {
    ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));
    for (int i = 0; i < HASHTABLE_CAPACITY; i++) {
        hmap->buckets[i] = NULL;
    }
    return hmap;
}

好的,谢谢,已经更改815a35c

krahets added a commit that referenced this issue Apr 11, 2024
* fix: Fix coding error, #440 (comment)

* fix: Fix issue 1237 #1237

* Update array_hash_map.c

* Update bucket_sort.c

* Update bucket_sort.c

* Update array_hash_map.c

* Update bucket_sort.c

* Update bucket_sort.c

---------

Co-authored-by: Yudong Jin <krahets@163.com>
@krahets krahets closed this as completed Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants