2D数组内存泄漏-应该很容易,我觉得愚蠢
2D Array Memory Leak - Should be easy, I feel stupid
很简单的代码:
signed int **ifftResults = (signed int **)malloc(sizeof(signed int *) * recordsPerBuffer);
for (int i=0; i < recordsPerBuffer; i++)
{
ifftResults[i] = (signed int*)malloc(sizeof(signed int) * fftLength);
}
之后:
for (int i=0; i < recordsPerBuffer; i++)
{
free(ifftResults[i]);
}
free(ifftResults);
当我注释掉这些行时-没有内存泄漏。当它们存在时——内存泄漏。希望我只是需要另一双眼睛,因为我这辈子都看不出哪里出了问题。
我写这篇文章时给出的代码似乎不足以回答"为什么"这个问题。
但是,由于您使用的是c++,您可以通过使用std::vector
来确保没有内存泄漏。
一样:
// Allocation.
std::vector< std::vector< int > > fftResults( recordsPerBuffer, std::vector< int >( fftLength ) );
// Usage:
fftResults[y][x] = blah;
// Deallocation: automatic.
另一种实现矩阵的方法是
std::vector< int > fftResults( recordsPerBuffer*fftLength );
,然后计算给定(x,y)的索引
干杯,hth。
相关文章:
- 使用Boost Interprocess创建托管共享内存需要很长时间
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 给定一个无符号整数,很容易交换每个偶数位和奇数位。这可以推广到交换每个偶数和奇数n位吗?
- C++内存管理很奇怪
- C 4D数组内存Deallocation速度很慢
- C 很容易使用枚举进行标志和位操作
- 当遇到很长的循环时,会出现分段错误:11 或内存分配错误
- 从某个特定但很大的虚拟地址空间(例如,20GB以上)分配内存
- C++:这个代码有什么问题吗?它编译得很好,但人们告诉我会有内存泄漏
- 为什么互斥体和条件变量很容易复制
- c++11中的内存建模测试,对memory_order_relaxed很好奇
- C++.Net程序集可以很容易地反编译吗
- 指针问题(可能很容易)
- 我可以很容易地创建一个模板函数,它接受任意类型的任意容器并对其进行操作
- 结构体向量的问题:解约束迭代器(可能很容易)
- 为什么 CUDA 8.0(有时)的内存访问很差,而 7.5 则没有?
- 2D数组内存泄漏-应该很容易,我觉得愚蠢
- VTK使用越来越多的内存,变得很慢
- 为什么内存管理很重要
- 以任何方式多次打开共享内存都很糟糕