C语言 释放已分配给字符指针(字符串)数组的内存。我必须释放每个字符串还是只释放“主”指针?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4733649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:32:50  来源:igfitidea点击:

Freeing memory which has been allocated to an array of char pointers (strings). Do I have to free each string or just the "main" pointer?

cmemorypointers

提问by caesius

I have a function that takes a pointer to a char ** and fills it with strings (an array of strings I guess). *list_of_strings* is allocated memory inside the function.

我有一个函数,它接受一个指向 char ** 的指针并用字符串填充它(我猜是一个字符串数组)。*list_of_strings* 是函数内部分配的内存。

char * *list_of_strings = NULL;

/* list_of_strings malloc'd inside function */
fill_strings_with_stuff(&list_of strings);

use_list_for_something(list_of_strings);

/* Now how do I free it all? */

How would I go about freeing the memory after I've used the strings? If I call

使用字符串后,我将如何释放内存?如果我打电话

free(list_of_strings);

won't that just free the actual pointers and not the memory each string itself was using? How do I completely free the memory

这不会释放实际的指针而不是每个字符串本身使用的内存吗?我如何完全释放内存

Just for clarity the function looks something like this:

为了清楚起见,该函数看起来像这样:

fill_strings_with_stuff(char *** list)
{
    *list = malloc(AMOUNT);

    for (i = 0; i < SOMETHING; i++) {
        *(list + i) = malloc(LINE_LEN);
        *(list + i) = some_string_from_somewhere
    }

    /* ... */

}

回答by Péter T?r?k

won't that just free the actual pointers and not the memory each string itself was using?

这不会释放实际的指针而不是每个字符串本身使用的内存吗?

Yes, indeed.

确实是的。

How do I completely free the memory

我如何完全释放内存

By looping through the array and freeing each string one by one beforefreeing up the array itself. E.g.

通过循环遍历数组并释放数组本身之前一个一个地释放每个字符串。例如

for (i = 0; i < SOMETHING; i++) {
    free(list[i]);
}
free(list);

回答by 0xCAFEBABE

Basically, there's a rule of thumb to allocating and freeing: You need to call as many free() as you called malloc(). It's as simple as that. In every other case you got yourself a memory leak.

基本上,分配和释放有一个经验法则:您需要调用与调用 malloc() 一样多的 free()。就这么简单。在其他所有情况下,您都会遇到内存泄漏。

回答by sharptooth

Yes, you have to free()every block you obtained from malloc(). You do it by traversing the array of pointers and caling free()on each element and only then freeing the array itself.

是的,您必须对free()malloc(). 您可以通过遍历指针数组并调整free()每个元素然后释放数组本身来实现。

Only you know that there's a tree-like structure that could be freed recursively, that knowledge is not anywhere in the C runtime heap, so the heap manager has no idea about that and your program has to free everything itself.

只有您知道存在可以递归释放的树状结构,该知识不在 C 运行时堆中的任何位置,因此堆管理器对此一无所知,您的程序必须自行释放所有内容。

回答by Daniel Gehriger

You need to iterate over listand call free()on every array member. Then free the array.

您需要迭代list并调用free()每个数组成员。然后释放阵列。