如果我像这样在循环中分配内存
for(file = 0; file < nfile; file++){
...
...
...
for(yy = 0; yy < ngridy; yy++){
for(xx = 0; xx < ngridx; xx++) {
tmparr1[xx+(ngridx*yy)] = (double *)calloc(nptperf[file], sizeof(double));
tmparr2[xx+(ngridx*yy)] = (double *)calloc(nptperf[file], sizeof(double));
}
}
稍后在代码中我会像这样释放内存:
for(yy = 0; yy < ngridy; yy++){
for(xx = 0; xx < ngridx; xx++) {
free(tmparr1[xx+(ngridx*yy)]);
free(tmparr2[xx+(ngridx*yy)]);
}
}
是否有可能 free()
不释放内存,从而导致分配更多内存?
我在每个 file
循环中分配和释放内存一次。此外,nptperf[file] 通常约为 1-3 百万点,ngridx = ngridy = 100。
此程序适用于 ngridx = ngridy = 80
及以下,但在 100
时失败。
最佳答案
您在循环体内使用了错误的变量(gg
和 ggy
而不是 xx
和 yy
>)。除其他问题外,这会导致(几乎)所有分配的内存泄漏,因为您丢失了 calloc()
ed 指针。
关于c - free()失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13285571/