如何释放char 3D数组?

时间:2021-03-26 15:18:35

标签: c memory-management

这样的字符串数组如何释放内存?

char** test[5];
for(int i=0;i< 5;i++)
        test[i] = malloc(sizeof(char*) * 4);
for(int i=0;i< 4;i++){
        test[0][i] = malloc(sizeof(char) * 50);
        test[1][i] = malloc(sizeof(char) * 50);
        test[2][i] = malloc(sizeof(char) * 50);
        test[3][i] = malloc(sizeof(char) * 50);
        test[4][i] = malloc(sizeof(char) * 50);
}

因为我做了类似的事情,我有内存泄漏

for (int i = 0; i <4; i++)
                {
                    free(test[0][i]);
                    free(test[1][i]);
                    free(test[2][i]);
                    free(test[3][i]);
                    free(test[4][i]);

                }
for (int i = 0; i < 5; i++)
                free(test[i]);

0 个答案:

没有答案