[英]how do I release a struct from memory and arrays within them
I want to know how I can release a struct properly from memory and completely wipe it. 我想知道如何从内存中正确释放结构并完全擦除它。 I also need to wipe arrays or values within this struct.
我还需要擦除此结构中的数组或值。 I tried just overwriting the struct with new data everytime the data changed but I observed a steady rise in memory use until I get a memory warning so I think my safest bet is to completely wipe the data first and then write to it again.
每次数据更改时,我都尝试用新数据覆盖结构,但是观察到内存使用量一直在稳定增长,直到收到内存警告,所以我认为最安全的选择是先完全擦除数据,然后再次写入。
typedef struct {
SInt16 *array1;
SInt16 *array2;
} myStruct, *myStructPtr;
myStructArray myStruct[16];
for(int i=0;i<16;i++)
{
myStruct[i].array1 =
(AudioUnitSampleType *) calloc (asize, sizeof (SInt16));
myStruct[i].array2 =
(AudioUnitSampleType *) calloc (asize, sizeof (SInt16));
}
free(myStructArray) // throws SIGBART error
You didn't malloc
or calloc
myStructArray
so you shouldn't free
it. 您没有
malloc
或calloc
myStructArray
所以您不应该free
它。 Loop over the elements and free
myStruct[i].array1
and array2
循环遍历元素并
free
myStruct[i].array1
和array2
for(int i=0;i<16;i++)
{
free(myStruct[i].array1);
free(myStruct[i].array2);
}
The general rule is simple - free
what you malloc/calloc/realloc/strdup/other allocs
, nothing more or less. 一般规则很简单-
free
您的malloc/calloc/realloc/strdup/other allocs
,不多多少少。 Note that alloca
is an exemption - it allocates on stack, so you should never free what you got from it. 请注意,
alloca
是一种豁免-它在堆栈上分配,因此您永远都不应释放从中获得的收益。
myStructArray myStruct[16];
myStruct
is an array of objects created on stack. myStruct
是在堆栈上创建的对象的数组。 You can not call free
on it. 您不能
free
拨打电话。 free
needs to be called on resources acquired from free store ( using malloc, realloc etc., ). free
需要被上(使用malloc,realloc的等)从自由存储区获得的资源调用。 Instead you need call free on struct members array1
, array2
. 相反,您需要对结构成员
array1
, array2
免费调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.