From what I understand, Segmentation Fault is when you haven't assigned memory properly yet, and Double free is when you try to free memory that you already freed?
What would be the proper way to increase the size of an array of Structs, and where/which parts do you actually need to free?
I have a struct:
struct Data {
// Some variables
}
and I'm initializing the array of those structs with:
int curEntries = 100;
int counter = 0;
struct Data *entries = (struct Data *)malloc(curEntries * sizeof(struct Data));
When I read data from a bin file into this array and populate each of the structs, the program works up until there are more than 100 structs needed. At that time, I have the following code to realloc the array:
if (counter == curEntries - 1) { // counter = current index, curEntries = size of the array
entries = (struct Data *)realloc(entries, curEntries * 2 * sizeof(struct Data));
// struct Data *temp = (struct Data *)realloc(entries, curEntries * 2 * sizeof(struct Data));
// free(entries);
// entries = temp;
// free(temp);
}
The line I'm using now (entries = . . . ) works, but is obviously wrong because I'm not freeing anything, right?
But when I tried using the commented out code instead, I got a double Free error
Finally, (because there are a series of automatic tests), apparently I need to use malloc and so forth in other parts of my code as well. Where else should I/do I need to assign memory?