Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

Tags:

c

pointers

memory

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 = 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
    }

    /* ... */

}
like image 902
caesius Avatar asked Jan 19 '11 09:01

caesius


People also ask

How do you free the memory of an array?

How do I free the memory associated with array I named "arr"? You should declare this as char arr[] = "bo" to allow the compiler to work out the length and so make sure that there is enough room for a null terminator. If you changed your code to char arr[3] = "boo"; then there would be no null terminator.

Should I free strings in C?

You allocate memory so you should free it. a="abc"; This assigns a pointer to a constant string to your char* a , by doing so you loose the pointer to the memory allocated in the first line, you should never free constant strings.

What does freeing a pointer do?

The function free takes a pointer as parameter and deallocates the memory region pointed to by that pointer. The memory region passed to free must be previously allocated with calloc , malloc or realloc . If the pointer is NULL , no action is taken.

How to free the memory in C?

“free” method in C is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place.


4 Answers

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 before freeing up the array itself. E.g.

for (i = 0; i < SOMETHING; i++) {
    free(list[i]);
}
free(list);
like image 53
Péter Török Avatar answered Oct 15 '22 10:10

Péter Török


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.

like image 40
0xCAFEBABE Avatar answered Oct 15 '22 10:10

0xCAFEBABE


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.

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.

like image 45
sharptooth Avatar answered Oct 15 '22 08:10

sharptooth


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

like image 37
Daniel Gehriger Avatar answered Oct 15 '22 08:10

Daniel Gehriger