I'm trying to dynamically allocate memory for an array of struct pointer in function. It works until 3 iteration but crash after with this error :
double free or corruption (fasttop): ...
Here is my struct pointer array declaration :
Intersection** alreadyUse = malloc(sizeof(Intersection*));
if(alreadyUse == NULL) {
exit(1);
}
int size = 1;
alreadyUse[0] = inter; // Pointer of an Intersection
// Some Code
checkFunction(alreadyUse, &size, interLeft);
And this is my function
bool checkFunction(Intersection** alreadyUse, int* size, Intersection* inter) {
for(int i = 0; i < *size; i++) {
if(alreadyUse[i] == inter) {
return true;
}
}
*size = *size +1;
Intersection** tmp = realloc(alreadyUse, sizeof(Intersection*) * *size);
if(tmp == NULL){
exit(1);
}
else {
alreadyUse = tmp;
}
alreadyUse[*size-1] = inter;
return false;
}
As I said, it works for 1, 2, 3 then I get the error.
Is someone have an idea why it works and then suddenly crash ?
Thanks for helping.