I know that this question exists in other places like:
pointer being freed was not allocated in C error: pointer being freed was not allocated
but, I'm still very confused. Errors seem to be associated with things like "modifying the original pointer returned by malloc" and "failing to malloc before freeing". I just don't see how these reasons apply to my program.
I'm writing a dynamically allocated array of strings:
#include <stdio.h>
#include <stdlib.h>
#define NLETTERS 25
typedef struct {
char** array;
size_t used;
size_t size;
} array_t;
array_t* initArray(size_t initialSize) {
array_t* a = malloc(sizeof(array_t));
a->array = malloc(initialSize*sizeof(char*));
a->used = 0;
a->size = initialSize;
int i;
for(i = 0; i < initialSize; i++) {
a->array[i] = malloc(sizeof(char) * NLETTERS);
}
return a;
}
void insertArray(array_t *a, char* element) {
if (a->used == a->size) {
a->size *= 2;
a->array = realloc(a->array, a->size * sizeof(char*));
int i;
for(i = (int)a->used; i < a->size; i++) {
a->array[i] = malloc(sizeof(char) * NLETTERS);
}
}
a->array[a->used++] = element;
}
void freeArray(array_t *a) {
int i;
for(i = 0; i < a->size; i++) {
free(a->array[i]);
}
free(a->array);
free(a);
a->array = NULL;
a->used = a->size = 0;
}
void print_array(array_t *a) {
int i;
for(i = 0; i < a->size; i++) {
printf("%s\n", a->array[i]);
}
}
int main(int argc, const char * argv[]) {
array_t *a;
a = initArray(2);
insertArray(a, "hello");
insertArray(a, "how are you");
print_array(a);
insertArray(a, "yup");
insertArray(a, "you know it");
print_array(a);
freeArray(a);
return 0;
}
When I attempt to "free", I get the error: "pointer being freed was not allocated" right at
free(a->array[0])
in the first iteration of the for-loop in freeArray();
Help would be greatly appreciated.