如何以这种方式释放C初始分味的Malloc 2D阵列?
我在C中宣布了这样的2D Malloc数组:
int** pArray;
int i;
pArray=(int**)malloc(pRows*sizeof(int*));
for(i=0;i<pRows;i++)
(int*)malloc(pColumns*sizeof(int*));
我该如何释放此数组?我在网上看到,free()的数量应与使用的malloc()数量相同。在这种情况下,我可以两次释放什么?
I have declared a 2D malloc array like this in C:
int** pArray;
int i;
pArray=(int**)malloc(pRows*sizeof(int*));
for(i=0;i<pRows;i++)
(int*)malloc(pColumns*sizeof(int*));
How can I free this array? I saw on the net that the number of free() should be same as number of malloc() used. What can I free twice in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于初学者来说,分配不正确。该语句
产生内存泄漏。
似乎您的意思是
要注意以下代码在循环中的语句中使用
sizeof(int)
而不是size> sizeof(int *)
。您需要以相反的顺序释放分配的内存
For starters the allocation is incorrect. This statement
produces a memory leak.
It seems you mean the following code
Pay attention to that in the statement within the loop there is used the expression
sizeof( int )
instead ofsizeof( int * )
.You need to free the allocated memory in the reverse order