简体   繁体   English

递归函数中的内存泄漏

[英]Memory leak in recursive function

This is a snippet of my implementation of the Cooley-Tukey algorithm in C. And yes, it's college homework. 这是我在C中实现Cooley-Tukey算法的片段。是的,它是大学作业。 But anyway... The algorithm works fine but I have to free ar1 and ar2 to get rid of immense memory leaks on huge input data but every time I try, I get invalid reads. 但无论如何......算法工作正常,但我必须释放ar1和ar2来摆脱巨大的输入数据上的巨大内存泄漏,但每次我尝试,我得到无效的读取。 In theory, ar1 and ar2 should only be used by the current instance of the function and they should be unique, since every instance mallocs its own output. 理论上,ar1和ar2应仅由函数的当前实例使用,并且它们应该是唯一的,因为每个实例malloc都有自己的输出。

complex_exp * dft(complex_exp * from, int N, int s, int inverse) {

if(N == 1)
    return from;

int i;
complex_exp * transformed = (complex_exp *) malloc(N * sizeof(complex_exp));
complex_exp * ar1 = dft(from, N / 2, 2*s, inverse); //LINE 83
complex_exp * ar2 = dft(from + s, N / 2, 2*s, inverse); // LINE 84

for(i = 0; i < N/2; i++) {

    transformed[i] = ar1[i]; //LINE 88
}

for(i = N/2; i < N; i++) {
    transformed[i] = ar2[i - N/2];
}

//Do stuff with the transformed array - NO reference to ar1 or ar2.

free(ar1); //LINE 113
return transformed;
}

Valgrind says: Valgrind说:

==69597== Invalid read of size 8
==69597==    at 0x100000EE6: dft (progtest05.c:88)
==69597==    by 0x100000EA2: dft (progtest05.c:84)
==69597==    by 0x100000E67: dft (progtest05.c:83)
==69597==    by 0x100000E67: dft (progtest05.c:83)
==69597==    by 0x100001A0E: main (progtest05.c:233)
==69597==  Address 0x100007250 is 64 bytes inside a block of size 256 free'd
==69597==    at 0xDCB8: free (vg_replace_malloc.c:450)
==69597==    by 0x1000011E5: dft (progtest05.c:113)
==69597==    by 0x100000E67: dft (progtest05.c:83)
==69597==    by 0x100000E67: dft (progtest05.c:83)
==69597==    by 0x100000E67: dft (progtest05.c:83)
==69597==    by 0x100001A0E: main (progtest05.c:233)

So it seems that a call to dft on line 83 frees the memory which then the call to dft on the next line tries to access. 因此,似乎第83行的dft调用释放了内存,然后下一行的dft调用尝试访问。 Any idea what's actually going on and how I could get rid of the leaks? 知道实际发生了什么以及如何摆脱泄漏?

You say "every instance mallocs its own output", however that's not true in this statement: 你说“每个实例mallocs都有自己的输出”,但是在这个声明中并不是这样:

if(N == 1)
    return from;

Perhaps when N==1 you should return a copy of from (ie malloc new memory, copy the contents of from into the new memory, and return the copy). 也许当N == 1时,你应该返回from的副本(即malloc新内存,将内容复制到新内存中,然后返回副本)。

how I could get rid of the leaks? 我怎么能摆脱泄漏?

I expect you must free ar1 and ar2 before returning transformed. 我希望你必须在返回转换之前释放ar1和ar2。

The best way to fix these issues is to clearly define your preconditions and postconditions. 解决这些问题的最佳方法是明确定义前置条件和后置条件。 Do you assume that the returned result has been malloc'ed? 你认为返回的结果是malloc'ed吗? If so, you appear to violate this by returning "from" and also by failing to free "ar2". 如果是这样,你似乎通过返回“from”并且也没有释放“ar2”来违反这一点。 If you assume that the returned result is not malloc'ed, then you need to ensure that this memory has been provided by the caller and furthermore not return malloc'ed memory. 如果你假设返回的结果不是malloc'ed,那么你需要确保调用者已经提供了这个内存,而且还没有返回malloc的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM