[英]How to free a 2D array allocated on N+1 mallocs ? In C
由于您使用的是 3 星指针,因此您需要在 function 中额外取消引用:
void FreeMatrix(int*** ptab, int N)
{
for (size_t i=0; i<N; i++)
{
// *ptab is an int** type, and that makes (*ptab)[i] an
// int* type. Presumably, you've malloced N int* types, so
// free each of those
free((*ptab)[i]);
}
// now free the initial int** allocation
free(*ptab);
// and set it to NULL per your requirements
*ptab= NULL;
}
请注意,3 星指针通常被认为是糟糕的设计
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.