You will need to call free for each of the elements of array
. array
is a static array(which is also the error you get attempt to free non-heap object
) and that is why you can not free
it. It's elements on the other hand are dynamically allocated and you can and should free them. Write a simple cycle and call free for the elements of array
in it.
EDIT: actually you can call free only on the dynamically allocated elements of array
. As you assign string literals to some of the elements and allocated memory dynamically for others(using malloc), you will have to use a helper array where you store a flag for each element that indicates if it is dynamically allocated or points to a string literal. After that only free the dynamically allocated elements in a cycle.