![](/img/trans.png)
[英]How to properly free dynamically allocated memory for an array of pointers to int's
[英]How to free a two dimensional array allocated memory using int ** ptr
如何使用函數分配內存使用int ** ptr釋放二維數組?
例如,我使用allocArray( &ptrArray, row, column);
分配數組。 使用此函數釋放已分配內存的正確過程是什么: void freeArray( int *** pA, int row, int column)
#include <stdio.h>
#include <stdlib.h>
void allocArray( int *** pA, int row, int column)
{
int i, j, count;
*pA = (int **) malloc(row * sizeof(int *));
for (int i =0; i<row; ++i)
{
(*pA)[i] = (int *) malloc( column * sizeof(int));
}
// Note that pA[i][j] is same as *(*(pA+i)+j)
count = 0;
for (i = 0; i < row ; i++)
for (j = 0; j < column; j++)
(*pA)[i][j] = ++count; // OR *(*(pA+i)+j) = ++count
for (i = 0; i < row; i++) {
for (j = 0; j < column; j++) {
printf("%d ", (*pA)[i][j]);
}
printf("\n");
}
}
// How to free a two dimensional array allocated memory using int ** ptr?
void freeArray( int *** pA, int row, int column)
{
}
void test_array_allocation()
{
int i, j;
int row = 3, column = 4;
int ** ptrArray;
allocArray( &ptrArray, row, column);
printf("test_array_allocation\n");
for (i = 0; i < row; i++) {
for (j = 0; j < column; j++) {
printf("%d ", (ptrArray)[i][j]);
}
printf("\n");
}
freeArray(&ptrArray, row, column); // free allocated memory
}
int main(int argc, const char * argv[]) {
test_array_allocation();
return 0;
}
如果你這樣做,你能告訴我你會怎么做嗎?
這是我將如何實現這一點。 我以前從未在C中實現過2d數組,有趣的代碼可以編寫。
#include <stdio.h>
#include <stdlib.h>
void*
xmalloc(size_t n)
{
void* p = malloc(n);
if (p == NULL)
{
printf("I handle my errors!\n");
exit(EXIT_FAILURE);
}
return p;
}
int**
alloc2dArray(unsigned rows, unsigned cols)
{
int** r = xmalloc(sizeof(int*) * rows);
for (int i = 0; i < rows; i++)
r[i] = xmalloc(sizeof(int) * cols);
return r;
}
void
print2dArray(int** a, unsigned rows, unsigned cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
printf("%4d", a[i][j]);
putchar('\n');
}
}
void
free2dArray(int** x, unsigned rows)
{
for (int i = 0; i < rows; i++)
free(x[i]);
free(x);
}
int main(void)
{
int** x = alloc2dArray(10, 14);
for (int i = 0; i < 10; i++)
for (int j = 0; j < 14; j++)
x[i][j] = i*j;
print2dArray(x, 10, 14);
free2dArray(x, 10);
return 0;
}
您可能不知道的另一個提示:您可以在GNU / Linux上使用valgrind
來驗證您是否正確解除了分配:
==9322== HEAP SUMMARY:
==9322== in use at exit: 0 bytes in 0 blocks
==9322== total heap usage: 11 allocs, 11 frees, 640 bytes allocated
==9322==
==9322== All heap blocks were freed -- no leaks are possible
==9322==
==9322== For counts of detected and suppressed errors, rerun with: -v
==9322== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
對於每次調用malloc
,都必須有相應的free
調用。 對free
的調用必須使用相應的對malloc
調用返回的指針值。
在您的情況下,您有以下使用malloc
分配內存的行。
*pA = (int **) malloc(row * sizeof(int *));
for (int i =0; i<row; ++i)
{
(*pA)[i] = (int *) malloc( column * sizeof(int));
}
使用存儲在(*pA)
的valus,必須有free
row
號。
然后,必須使用*pA
進行一次free
調用。
現在,您可以將freeArray
實現為:
// No need for using int ***. You are not going to modify pA
// You are just going to use the pointer.
// You don't need column in this function.
void freeArray( int ** pA, int row)
{
for (int i =0; i<row; ++i)
{
free(pA[i]);
}
free(pA);
}
並使用以下命令從test_array_allocation
調用它:
freeArray(pA, row);
這個功能:
void freeArray( int *** pA, int row, int column)
{
}
不需要參數“列”。
void freeArray( int **pA, int row )
{
for( int i = 0; i < row; i++ )
{
free( pA[i] );
}
free( pA );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.