Search code examples
cpointersmallocreallocmemory-corruption

Realloc corruption after some iteration C


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.


Solution

  • In this function call

    checkFunction(alreadyUse, &size, interLeft);
    

    the variable size is passed by reference. So it can be changed in the function. However as you see the variable alreadyUse is not passed by reference. So the function deals with a copy of the value of the variable. If you want that the variable would be changed in the function you have to pass it by reference

    checkFunction( &alreadyUse, &size, interLeft);
                   ^^^^^^^^^^^
    

    Thus the function should be declared like

    bool checkFunction(Intersection*** alreadyUse, int* size, Intersection* inter);
                       ^^^^^^^^^^^^^^^
    

    The function definition can look like

    bool checkFunction( Intersection ***alreadyUse, int *size, Intersection *inter ) 
    {
        for ( int i = 0; i < *size; i++ ) 
        {
            if ( alreadyUse[0][i] == inter ) return true;
        }
    
        Intersection **tmp = realloc( alreadyUse[0], sizeof( Intersection * ) * ( *size + 1 ) );
    
        if ( tmp == NULL ) exit( 1 );
    
        alreadyUse[0] = tmp;
    
        alreadyUse[0][( *size )++] = inter;
    
        return false;
    }