如何免费的C二维数组 [英] how to free c 2d array

查看:91
本文介绍了如何免费的C二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code:

int **ptr = (int **)malloc(sizeof(int*)*N); 
for(int i=0;i<N;i++) 
     ptr[i]=(int*)malloc(sizeof(int)*N));

我怎么可以免费使用PTR 免费?我应该遍历PTR和免费PTR [I]?或者只是做

how can i free ptr using free? should i loop over ptr and free ptr[i]? or just do

free(ptr) 

和PTR将得到释放?

推荐答案

您将不得不遍历PTR [I],释放你遍历每个为int *,因为你第一次提示。例如:

You will have to loop over ptr[i], freeing each int* that you traverse, as you first suggest. For example:

for (int i = 0; i < N; i++)
{
    int* currentIntPtr = ptr[i];
    free(currentIntPtr);
}

这篇关于如何免费的C二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆