I have a struct that contains an array of strings (char arrays) and an int for the maximum capacity of the array.
typedef struct ArrayList
{
char **array;
int capacity;
}
The array is initialized with malloc in its own method.
list->array = malloc(sizeof(char *) * templength);
And the individual strings are initialized as
list->array[nextEmptyString] = malloc(sizeof(str));
strcpy(list->array[nextEmptyString], str);
I need to be able to clear everything about that array from memory and then pass a new pointer to that array, however when I attempt to free the individual strings before freeing the array it scrambles the new array.
for (word = 0; word < list->capacity; word++)
free(list->array[word]);
free(list->array);
list->array = newArray;
Where list is an ArrayList and newArray is an array of strings. The code runs correctly if I comment out the for loop, but then don't i just have a bunch of orphaned strings floating around in memory?
The intended output is something like Adding Pierre to L1 ...
and instead I get Adding �c� to L1 ...