[英]free on malloc'ed array segmentation fault
I'm having trouble when freeing a previously malloc'd array, it throws a seg-fault 释放以前的malloc数组时遇到麻烦,它会引发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: 我尝试在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)
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. 我将在此处添加在Main中调用的函数,这些函数可以更改那些已分配内存的区域,但是我担心主帖子是否会被阻塞。 If so, I'll upload some code to pastebin and link it here.
如果是这样,我将一些代码上传到pastebin并在此处链接。
Thanks in advance. 提前致谢。
sound struct: 声音结构:
typedef struct snd{
unsigned char *bytes;
int size;
}SOUND;
freeSound: freeSound:
void freeSound(SOUND **sound){
if(sound == NULL || *sound == NULL) return;
free((*sound)->bytes);
free(*sound);
sound = NULL;
}
readSoundFromFile: 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: 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: 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 编辑:忘记添加声音结构
quickSort(out, 0, getSize(sound)-1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.