Kasama
Kasama

Reputation: 83

free on malloc'ed array segmentation fault

I'm having trouble when freeing a previously malloc'd array, it throws a seg-fault

int main(int argc, char *argv[]){

    SOUND *sound;
    double *out;
    char filename[128];
    int i, k;

    scanf("%s", filename);

    //Read the file 'filename' and save the bytes into the SOUND stuct
    sound = readSoundFromFile(filename);

    out = (double*) malloc(sizeof(double)*getSize(sound));

    scanf("%d", &k);

    //Discrete Cossine Transform
    dct(sound, out);

    //Simple Quick Sort implementation
    quickSort(out, 0, getSize(sound));

    //print the output to the screen
    for(i = 0; i < k; i++){
        printf("%.2lf\n", out[(getSize(sound))-i]);
    } 

    freeSound(&sound); //Both of these throw seg fault
    free(out);

    return 0;

}

I do not know why it throws the segfault, I do not lose the pointer, at least I do not believe I do.

I tried running the program on valgrind and the output says that there is a invalid read of size 8, as follows:

==18962== Invalid read of size 8
==18962==    at 0x400B45: quickSort (in main)
==18962==    by 0x400C0F: quickSort (in main)
==18962==    by 0x400DF7: main (main.c:28)
==18962==  Address 0x54dea68 is 0 bytes after a block of size 1,608 alloc'd
==18962==    at 0x4C29F90: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18962==    by 0x400DA8: main (main.c:20)

I'll add here the functions I call in Main that can be changing those malloc'd memory regions, but I worry if the main post will get too clogged up. If so, I'll upload some code to pastebin and link it here.

Thanks in advance.


sound struct:

typedef struct snd{
    unsigned char *bytes;
    int size;
}SOUND;

freeSound:

void freeSound(SOUND **sound){

    if(sound == NULL || *sound == NULL) return;

    free((*sound)->bytes);
    free(*sound);
    sound = NULL;

}

readSoundFromFile:

SOUND *readSoundFromFile(char *filename){

    SOUND *sound;
    FILE *file;

    sound = (SOUND *) malloc(sizeof(SOUND));
    file = fopen(filename, "rb");

    //move the file cursor to EOF and ftell the position it's in (filesize in bytes)
    fseek(file, 0L, SEEK_END);
    sound->size = ftell(file);
    //move the cursor back
    fseek(file, 0L, SEEK_SET);
    sound->bytes = (unsigned char *) malloc(sizeof(unsigned char)*sound->size);

    //read sound->size bytes (unsigned char) from file, into sound->bytes
    fread(sound->bytes, sizeof(unsigned char), sound->size, file);

    fclose(file);

    return sound;
}

dct:

void dct(SOUND *sound, double *out){

    int n, i, j;
    double k;

    n = 0;
    for (i = 0; i < getSize(sound); i++){
        k = 0;
        for (j = 0; j < getSize(sound); j++){
            k += getByteValue(sound, j) * cos( (M_PI/getSize(sound)) * i * (j +0.5) ); // discrete cossine transform II formula 
        }
        //setSoundByte(out, i, k);
        out[i] = k;
    }

}

quickSort:

void quickSort(double *a, int left, int right){

    int currentLeft, currentRight;
    double midElement, tmp;

    currentLeft = left;
    currentRight = right;
    midElement = a[(left+right)/2];

    while(currentLeft <= currentRight) {

        while(a[currentLeft] < midElement && currentLeft < right) currentLeft++;
        while(a[currentRight]> midElement && currentRight > left) currentRight--;

        if(currentLeft <= currentRight){
            swap(&(a[currentLeft]),&(a[currentRight]));
            currentLeft++;
            currentRight--;
        }
    }

    if (currentRight > left) quickSort(a, left, currentRight);
    if (currentLeft < right) quickSort(a, currentLeft, right);

}

EDIT: forgot to add the SOUND struct

Upvotes: 0

Views: 396

Answers (1)

Michael Burr
Michael Burr

Reputation: 340208

quickSort(out, 0, getSize(sound)-1)

Upvotes: 3

Related Questions