我有一个问题,弄清楚为什么我在下面的代码中肯定会丢失错误。我已经在主函数中将return 0更改为exit(0),但它没有帮助。
struct s* function(int K,struct graph *G,int c){
uint32_t *a;
a=(uint32_t*)calloc(K+2,sizeof(uint32_t));
int *b;
b=(uint32_t*)calloc(K+2,sizeof(uint32_t));
return s;
}
int main(int argc, char *argv[])
{
struct S* s=function(K,Gprime,capacity);
return 0;
}
以下您可以看到S结构代码:
struct S{
int *S;
uint32_t *Scount;
};
struct S* Sp(int s,int k){
struct S* sz =malloc(sizeof (struct S));
sz->S = (int*)calloc(s+1, sizeof(int));
sz->scount=(uint32_t*)calloc(k+1,sizeof(uint32_t));
return sz;
}
这是valgrind错误:
==5343== 680 bytes in 5 blocks are definitely lost in loss record 10 of 52
==5343== at 0x4C272B8: calloc (vg_replace_malloc.c:566)
==5343== by 0x40106B: function (reflowk.c:3)
==5343== by 0x402BB0: main (reflowk.c:9)
答案 0 :(得分:2)
完成使用该内存后,您必须调用free()
释放malloc
和calloc
保留的内存。