[英]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)的索引。
干杯,……
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.