Search code examples
cfreecorruption

c - double free or corruption where there arent frees


Well, i have done the fallowing code ( basically want i want to do is: i have a list where i have some obstacles, if x=-1, the obstacles are in an entire floor, else, they are only at one place, so i want to make each time i go through the code, that the obstacles are in that position):

void restartmatrix(parking * parklot, block * obstacles, int time) {
    int x, y, z;
    block * tempblock;
    for (z = 0; z < parklot->depth; z++) {
        for (x = 0; x < parklot->width; x++) {
            for (y = 0; y < parklot->height; y++) {
                (parklot - > floors[x][y][z]).percorrido = -1;
            }
        }
    }
    /*tempblock = obstacles;
    while(tempblock != NULL){
        if(tempblock->start = time){
            if(tempblock->x != -1){
                for(x=0; x < parklot->width; x++){
                    for(y = 0; y < parklot->height; y++){
                        (parklot->floors[x][y][tempblock->z]).percorrido = 1;
                    }
                }
            }else{
                (parklot->floors[tempblock->x][tempblock->y][tempblock->z]).percorrido = 1;
            }
        }else if(tempblock->end == time && tempblock->end != 0){
            if(tempblock->x != -1){
                for(x=0; x < parklot->width; x++){
                    for(y = 0; y < parklot->height; y++){
                        (parklot->floors[x][y][tempblock->z]).percorrido = -1;
                    }
                }
            }else{
                (parklot->floors[tempblock->x][tempblock->y][tempblock->z]).percorrido = -1;
            }
        }
    tempblock = tempblock->next;
    }*/
}

Has you can see some part of the code is commented....well, the problem is if uncomment it, the program gives me a double free or corruption error (fasttop) but i can't seem to find why, since i haven't done any free...

Any help would be appreciated

EDIT

The code where i allocate the list of obstacles is here, if it can help:

block * read_blocking_list(char * file){
FILE * fp;
char leitura[256];  
char temporary;
block * head = NULL;
block * temp;
block * aux;
int start, end, x = 0, y = 0, z = 0, size;

fp = fopen(file, "r");
if(fp == NULL){
    printf("Erro ao abrir o ficheiro de leitura\n");
    exit(0);
}
while(fgets(leitura, 255, fp) != NULL){
    temp = malloc(sizeof(block));
    size = sscanf(leitura, "%c %d %d %d %d %d", &temporary, &start, &end, &x, &y, &z);
    temp->start = start;
    temp->end = end;
    temp->next = NULL;
    if(size < 5){
        temp->x = -1;
        temp->z = x;
    }else{
        temp->x = x;
        temp->y = y;
        temp->z = z;
    }
    if(head == NULL){
        head = temp;
    }else{
        aux = head;
        while(aux->next != NULL){
            aux = aux->next;
        }
        aux->next = temp;
    }
}
fclose(fp);
return head;
}   

Solution

  • Careful of = versus ==:

    if(tempblock->start = time){
    

    should be

    if(tempblock->start == time){
    

    Also, this block of code:

    if(tempblock->x != -1){
        for(x=0; x < parklot->width; x++){
            for(y = 0; y < parklot->height; y++){
                (parklot->floors[x][y][tempblock->z]).percorrido = 1;    
            }
        }
    }else{
        (parklot->floors[tempblock->x][tempblock->y[tempblock>z]).percorrido = 1;
    }
    

    Is like saying:

    if(tempblock->x != -1){
        /* Do some stuff here while tempblock->x is NOT -1. */
    }else{
        /* So in this block, tempblock->x IS EQUAL TO -1. */
        (parklot->floors[tempblock->x][tempblock->y][tempblock->z]).percorrido = 1;
    }
    

    So, trying to clearly illustrate where that leaves us, we have:

    if(tempblock->x != -1){
        /* Do some stuff here while tempblock->x is NOT -1. */
    }else{
        /* So in this block, tempblock->x IS EQUAL TO -1. */
        (parklot->floors[-1][tempblock->y][tempblock->z]).percorrido = 1;
    }
    

    Notice the parklot->floors[-1]. You are definitely out of bounds.