Reputation: 391
I'm trying to figure out how to free the memory for an array of character pointers (string literals), but I can't quite get the syntax. This is how I'm declaring and initializing the arrays.
char * words[] = { "THESE", "ARE", "SOME", "WORDS" };
I've tried doing this...
free(words);
And this...
for(i = 0; i < 4; i++) {
free(words[i]);
}
But the first one causes some sort of invalid pointer error with glibc, and the second one causes a segmentation fault.
So what's the right way to free this memory?
Upvotes: 0
Views: 84
Reputation: 2301
If you did not allocate it (e.g malloc
, calloc
) then you shouldn't be deallocating it either (e.g free
).
Documentation will in general be very clear and explicit if you need to free a pointer returned from a function, for example strdup
would be such a case.
Upvotes: 3