简体   繁体   English

释放malloc结构的数组时出错

[英]Error while freeing a malloc'd array of structs

I'm having issues freeing a temporary array of structs used for doubling the size of another array. 我在释放用于将另一个数组的大小加倍的临时结构数组时遇到问题。 I don't seem to have any issues freeing the original array. 我似乎没有任何问题释放原始数组。

void foo(StruName **structName,i nt *size)
{ 
...
 StruName *temp_array = (StruName*) malloc(*size * 2 * sizeof(StruName));
 for (i = 0; i < *size; i++)
   temp_array[i] = (*original_array)[i];
 free(*original_array);
 *original_array = temp_array;
 free(*temp_array);

I get the following error using g++ -Wall 我使用g ++ -Wall得到以下错误

error: cannot convert ‘StruName’ to ‘void*’ for argument ‘1’ to ‘void free(void*)’

Any ideas what could be causing this? 任何想法可能是什么原因造成的? If I leave the free(*temp_array); 如果我离开自由(* temp_array); out altogether, the program compiles and runs fine. 总共,程序可以编译并运行良好。 free(temp_array); 免费(temp_array); causes a segfault 导致段错误

The following part of function definition is not correct : 函数定义的以下部分不正确:

void foo(Struct **structName, int count, int *size)

Have you declared your structure as structName ? 您是否已将结构声明为structName If yes, you need to mention the parameter as well in the definition, not just its type.And even if structName is your structure,then you are expected to write struct structName** instead of Struct **structName .( Struct is not the same as struct in C) 如果是的话,您还需要在定义中同时提及参数,而不仅仅是其类型。即使structName是您的结构,那么您也应该编写struct structName**而不是Struct **structName 。( Struct不是与C中的struct相同)

Here's one more error: 这是另一个错误:

malloc(*size * 2 * sizeof(original_array)

You should enclose *size within braces as it's not clear whether you are dereferencing size or you are dereferencing the size*2*sizeof(original_array) 您应该将*size括在括号中,因为尚不清楚是要取消引用size还是要取消引用size*2*sizeof(original_array)

What's the reason that original_array is a pointer to a pointer? original_array是指向指针的原因是什么?

Anyways, it seems to me you shouldn't be freeing the temp array as your code snippet seems to imply you're replacing *original_array with it. 无论如何,在我看来,您不应该释放temp数组,因为您的代码段似乎暗示您正在用它替换*original_array If that's the case then you are working with freed memory; 如果是这种情况,则说明您正在使用释放的内存。 this might run fine for a while, but eventually you might find your array values get overwritten, as the freed memory gets reassigned to something else. 这可能会运行一会儿,但是最终您可能会发现数组值被覆盖,因为释放的内存被重新分配给其他对象。

// here *original_array will be pointing to the new `malloc`ed memory
*original_array = temp_array;

// After this line, *original_array is left dangling to the recently freed memory

free(*temp_array);

I would remove that last free line. 我将删除最后一条空闲线。

*Edit * I previously mentioned the memory would be freed twice, this is not the case, as free should have been called without dereferencing *temp_array in order for that to be true. *编辑*我之前提到过,内存将被释放两次,事实并非如此,因为应该在不取消引用* temp_array的情况下调用free以便使内存为真。

It's the basic principle of memory use that free what you have allocated. 释放所分配的内容是内存使用的基本原理。 If you free what you haven't allocated, it might cause segmentfault. 如果释放未分配的内容,则可能会导致segmentfault。

The code have allocated the memory with: 代码已使用以下方式分配了内存:

    StruName *temp_array = (StruName*) malloc(*size * 2 * sizeof(StruName));

Then it should release the memory with: 然后,它应该使用以下方法释放内存:

    free(*temp_array);

Make sure the content of both parameters are correct. 确保两个参数的内容正确。

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

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