C中的免费2D数组 [英] free 2d array in c

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

问题描述

遇到以下示例时,我正在解决一些简单的指针练习:

I was solving some simple pointer exercises when i came across the following example:

void deallocate2D(int** array, int nrows) {     
 /*  deallocate each row  */
 int i;
 for(i = 0; i < nrows; i++) {
      free(array[i]);
 }

 /*  deallocate array of pointers  */
 free(array);     
}

该数组在main中定义为int ** array1;

The array is defined in main as int** array1;

这是释放2d数组的内存的正确方法还是应该在函数中传递int ***数组? 有没有办法检查内存是否已成功释放?

Is this a correct way of deallocating memory of a 2d array or should an int*** array be passed in the function instead? Is there a way to check that memory has been successfully deallocated?

推荐答案

是的,这是正确的. int***仅在您打算更改指针的值时才需要,而不必更改.

Yes, that's correct. int*** is only necessary if you intend to change the value of the pointer, which you don't need to.

确实存在可以检查您的内存分配/释放的工具-Valgrind应该能够做到.

There do exist tools which can check your memory allocations/frees - Valgrind should be able to do it.

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

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