I am learning C and I can't free a dynamically allocated array of structs. Here is my sample code:
typedef char Str50[50];
typedef struct exam {
Str50 firstname;
Str50 lastname;
Str50 className;
int score;
Str50 date;
} Exam;
Exam *examDB
size_t allocateAndFreeTheStructArray {
size_t numRecs = 100; //let's say the array contains 100 elements
examDB = malloc(numRecs * sizeof * examDB); //this allocates the needed memory
// the code below tries to free what malloc allocated
for (size_t i = 0; i < numRecs; i++) {
free(&examDB[i]);
}
free(examDB);
}
When I compile this for windows, everything works fine (it seems). When I compile the same piece of code on my mac using xcode, I get an error that says: "malloc: *** error for object 0x7fae610060d0: pointer being freed was not allocated"
How is that possible? I used malloc to create the array... What am I missing here?