c - free on malloc'ed 数组段错误

标签 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;

自由之声:

void freeSound(SOUND **sound){

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

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

}

从文件读取声音:

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

}

编辑:忘记添加 SOUND 结构

最佳答案

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

关于c - free on malloc'ed 数组段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26557217/

相关文章:

c - 为什么我在函数 (C) 的末尾出现段错误?

C++ char* 到字符串运行时错误

php - 从 C 调用 PHP 运行时函数作为链接库

c - 如何检查与网络驱动器的连接?

C相当于autoflush(每次写入后刷新stdout)?

c - fputs 中的 fgets 给出段错误

c - 结构中数组的 malloc 作为参数传递

计算输入一个单词的次数

c - 二叉搜索树中的段错误(核心转储)

c - 段错误(核心转储)和 zlib