C - 在错误定位阵列分段错误时免费



我在释放以前malloc的数组时遇到问题,它抛出了一个seg错误

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("%.2lfn", 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;
    }
}

快速排序:

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:忘记添加SOUND结构

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

相关内容

  • 没有找到相关文章

最新更新