为什么我的动态字符串数组的实现会泄漏?

发布于 2024-12-11 17:40:13 字数 1831 浏览 0 评论 0原文

我需要一个字符串数组,当添加更多项目时,它会动态调整大小。我的基本代码可以工作,但 valgrind 报告内存泄漏。

应该使用静态和动态分配的字符串的实现如下所示:

typedef struct {
    char **items;
    int num;
} StringArray;

StringArray* string_array_init() {
    StringArray *arr = malloc(sizeof(StringArray));
    arr->items = NULL;
    arr->num = 0;
    return arr;
}

void string_array_add(StringArray *arr, char *str, int str_len) {
    void *new_items = realloc(arr->items, (arr->num + 1) * sizeof(char *));

    arr->items = (char**)new_items;
    arr->items[arr->num] = strndup(str, str_len);
    arr->num++;
}

void string_array_cleanup(StringArray *arr) {
    int i;

    for (i = 0; i < arr->num ; i++) {
        free(arr->items[i]);
    }

    free(arr);
}




int main() {
    StringArray *arr = string_array_init();

    string_array_add(arr, "item 1", strlen("item 1"));
    string_array_add(arr, "item 2", strlen("item 2"));
    string_array_add(arr, "item 3", strlen("item 3"));

    string_array_cleanup(arr);

    return 0;
}

Valgrind 报告:

==31443== HEAP SUMMARY:
==31443==     in use at exit: 12 bytes in 1 blocks
==31443==   total heap usage: 7 allocs, 6 frees, 53 bytes allocated
==31443== 
==31443== 12 bytes in 1 blocks are definitely lost in loss record 1 of 1
==31443==    at 0x4025CCD: realloc (vg_replace_malloc.c:525)
==31443==    by 0x80484A6: string_array_add (in ...)
==31443==    by 0x8048593: main (in ...)
==31443== 
==31443== LEAK SUMMARY:
==31443==    definitely lost: 12 bytes in 1 blocks
==31443==    indirectly lost: 0 bytes in 0 blocks
==31443==      possibly lost: 0 bytes in 0 blocks
==31443==    still reachable: 0 bytes in 0 blocks
==31443==         suppressed: 0 bytes in 0 blocks
==31443== 

为什么 realloc 泄漏以及如何修复它?我认为单独释放每个字符串然后释放结构就足够了,但我错过了一些东西。

I need a string array which dynamically resizes when more items are added to it. I got the basic code working, but valgrind reports memory leaks.

The implementation which should work with static and dynamically allocated strings looks like this:

typedef struct {
    char **items;
    int num;
} StringArray;

StringArray* string_array_init() {
    StringArray *arr = malloc(sizeof(StringArray));
    arr->items = NULL;
    arr->num = 0;
    return arr;
}

void string_array_add(StringArray *arr, char *str, int str_len) {
    void *new_items = realloc(arr->items, (arr->num + 1) * sizeof(char *));

    arr->items = (char**)new_items;
    arr->items[arr->num] = strndup(str, str_len);
    arr->num++;
}

void string_array_cleanup(StringArray *arr) {
    int i;

    for (i = 0; i < arr->num ; i++) {
        free(arr->items[i]);
    }

    free(arr);
}




int main() {
    StringArray *arr = string_array_init();

    string_array_add(arr, "item 1", strlen("item 1"));
    string_array_add(arr, "item 2", strlen("item 2"));
    string_array_add(arr, "item 3", strlen("item 3"));

    string_array_cleanup(arr);

    return 0;
}

Valgrind reports:

==31443== HEAP SUMMARY:
==31443==     in use at exit: 12 bytes in 1 blocks
==31443==   total heap usage: 7 allocs, 6 frees, 53 bytes allocated
==31443== 
==31443== 12 bytes in 1 blocks are definitely lost in loss record 1 of 1
==31443==    at 0x4025CCD: realloc (vg_replace_malloc.c:525)
==31443==    by 0x80484A6: string_array_add (in ...)
==31443==    by 0x8048593: main (in ...)
==31443== 
==31443== LEAK SUMMARY:
==31443==    definitely lost: 12 bytes in 1 blocks
==31443==    indirectly lost: 0 bytes in 0 blocks
==31443==      possibly lost: 0 bytes in 0 blocks
==31443==    still reachable: 0 bytes in 0 blocks
==31443==         suppressed: 0 bytes in 0 blocks
==31443== 

Why is realloc leaking and how can I fix it? I thoght freeing every string separately and then freeing the struct would be enough, but I´m missing something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国粹 2024-12-18 17:40:13

您释放了 arr->items 中包含的字符串,但没有释放 arr->items 本身。 (您在string_array_add中分配了它)。

You free the strings contained in arr->items, but you don't free arr->items itself. (You allocated it in string_array_add).

沧桑㈠ 2024-12-18 17:40:13

问题出在这一行:

arr->items = (char**)new_items;

您覆盖了旧指针。您需要重新分配此指针而不是 new_items。

The problem is with this line:

arr->items = (char**)new_items;

You overwrite the old pointer. You need to realloc this pointer and not new_items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文