Dav*_*uez 3 c memory-leaks memory-management
我知道StackOverflow上有很多关于这个主题的答案,他们非常有帮助,但我不确定一件事.假设我有funcA和funcB:
char * funcA()
{
char * arr = malloc(sizeof(char) * 20);
//store data in arr...
return arr;
}
void funcB()
{
char * arr;
while(up to some finite value)
{
arr = funcA();
//do stuff with array
/* Do I perform these two lines here every iteration?*/
free(arr)
arr = NULL;
}
/*Or do I perform it only once, at the end of the loop?*/
free(arr)
arr = NULL;
}
Run Code Online (Sandbox Code Playgroud)
我想arr应该在每次迭代中被释放.或者每次迭代都会"覆盖"数据arr,从而使得在循环结束时只能自由调用是安全的吗?这样做的正确方法是什么?谢谢.