C语言 如何使用 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_allocationn");
    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));
}

必须有row调用free的次数,使用存储在(*pA)中的价值。

然后,必须有一个调用free,使用*pA

现在,您可以将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 );
}

最新更新