c - free()失败

标签 c memory-management calloc

如果我像这样在循环中分配内存

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 时失败。

最佳答案

您在循环体内使用了错误的变量(ggggy 而不是 xxyy >)。除其他问题外,这会导致(几乎)所有分配的内存泄漏,因为您丢失了 calloc()ed 指针。

关于c - free()失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13285571/

相关文章:

c - 将 float 舍入并乘以它的字符(c)* 更新

c - 在 C 中使用 static const unsigned long long int 变量分配 static const double

c - Makefile - 在单独的文件中包含路径

c - malloc 和 calloc 的区别?

Calloc分配不一致

c - C语言中strtok的使用

memory-management - Elixir/Erlang 如何管理函数调用中传递的变量的内存?

linux - 检索 Linux 上单个进程的 CPU 使用率和内存使用率?

c++ - 使用 Fortran 中的内存数据调用 C 代码

C 中动态二维数组分配的正确解释