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?

后端 未结 4 910
春和景丽
春和景丽 2021-02-15 11:26

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.



        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-15 11:58

    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);
    

提交回复
热议问题