免费使用malloc' ed数组分段错误

时间:2014-10-24 22:26:04

标签: c segmentation-fault malloc free

我在释放以前的malloc数组时遇到麻烦,它会抛出一个段错误

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;

}

我不知道为什么会抛出段错误,我不会丢失指针,至少我不相信我这样做。

我尝试在valgrind上运行程序,输出显示大小为8的读取无效,如下所示:

==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)

我在这里添加我在Main中调用的函数,这些函数可以更改那些malloc内存区域,但我担心主帖是否会过于堵塞。如果是这样,我会将一些代码上传到pastebin并在此处链接。

提前致谢。


声音结构:

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);

}

编辑:忘了添加SOUND结构

1 个答案:

答案 0 :(得分:3)

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