相关疑难解决方法(0)

退出前我应该释放内存吗?

因错误而退出程序时,我应该释放所有的mallocated内存吗?

something = (char**) malloc (x * sizeof(char*));
for (i = 0; i < x; i++)
    something[i] = (char*) malloc (y + 1);

...

if (anything == NULL) {
   printf("Your input is wrong!");
   // should I free memory of every mallocated entity now?
   exit(1);
} 
else {
   // work with mallocated entities
   ...
   free(something); // it must be here
   system("pause);
}
Run Code Online (Sandbox Code Playgroud)

c malloc free dynamic-allocation

20
推荐指数
4
解决办法
6239
查看次数

标签 统计

c ×1

dynamic-allocation ×1

free ×1

malloc ×1