c-分配和*2D阵列

时间:2022-09-06 12:00:34

I am trying to write 2 functions, one to Dynamic allocation a 2D array,Other to free this 2D array:

我正在尝试编写2个函数,一个用于动态分配一个2D数组,另一个用于释放这个2D数组:

int allocate(int **array, unsigned int rows, unsigned int columns){
  int i;
  for (i = 0; i < rows; i++) {
    array[i] = malloc(columns * sizeof (int));
  }
  /* Code fo fill the array*/
  return 1;
}
void de_allocate(int **v, unsigned int rows) {
  int i;
  for (i = 0; i < rows; i++) {
    free(v[i]);
  }
  free(v);
}
int main(int argc, char **argv) {
    int rows, columns;
    rows = atoi(argv[1]);
    columns = atoi(argv[2]);
    int *ipp[rows];
    allocate(ipp, rows, columns);
    de_allocate(ipp,rows);  
    return 0;
}

I must respect the allocate function signature :

我必须尊重分配函数签名:

int allocate(int **array, unsigned int rows, unsigned int columns)

And at the end of allocate function ipp must have access to allocated 2D array.

在分配函数结束时,ipp必须能够访问分配的2D数组。

Allocate function it's right but in de_allocate function i have a SIGABRT Signal

分配功能是正确的,但在de_allocate功能中,我有一个SIGABRT信号

2 个解决方案

#1


1  

The problem is that you are trying to free a stack allocated var with code free(v);

问题是你试图释放一个堆栈分配var与代码free(v);

You could do that if you mallocated the array of pointer, but you declare it locally in main function with int *ipp[rows];

你可以这样做,如果你mallocated指针数组,但你在主函数中使用int * ipp [rows]在本地声明它;

Change it to int **ipp = malloc(sizeof(int*)*rows); if you want to leave de_allocate as is.

将其更改为int ** ipp = malloc(sizeof(int *)* rows);如果你想保留de_allocate原样。

You could test it with

你可以测试一下

#include <stdio.h>
#include <stdlib.h>

int allocate(int **array, unsigned int rows, unsigned int columns){
  int i;
  for (i = 0; i < rows; i++)
  {
    array[i] = malloc(columns * sizeof (int));
  }

  /* Code fo fill the array*/
  return 1;
}

void de_allocate(int **v, unsigned int rows) {
  int i;
  for (i = 0; i < rows; i++)
  {
    free(v[i]);
  }
  free(v);
}

int main(int argc, char **argv) 
{
    int rows, columns;
    int temp = 0;
    rows = atoi(argv[1]);
    columns = atoi(argv[2]);

    int **ipp = malloc(sizeof(int*)*rows);

    allocate(ipp, rows, columns);

    for (int i=0; i<rows; i++)
        for (int j=0; j<columns; j++)
            ipp[i][j] = temp++;

    for (int i=0; i<rows; i++)
        for (int j=0; j<columns; j++)
            printf("ipp[%d][%d] = %d\n", i, j, ipp[i][j]);

    de_allocate(ipp,rows);
    return 0;
}

#2


1  

void de_allocate(int **v, unsigned int rows) {
  int i;
  for (i = 0; i < rows; i++) {
    free(v[i]);
  }
  free(v);
-----^----
here you are attempting to free a variable for which you didn't dynamically allocate memory in the first place

}

#1


1  

The problem is that you are trying to free a stack allocated var with code free(v);

问题是你试图释放一个堆栈分配var与代码free(v);

You could do that if you mallocated the array of pointer, but you declare it locally in main function with int *ipp[rows];

你可以这样做,如果你mallocated指针数组,但你在主函数中使用int * ipp [rows]在本地声明它;

Change it to int **ipp = malloc(sizeof(int*)*rows); if you want to leave de_allocate as is.

将其更改为int ** ipp = malloc(sizeof(int *)* rows);如果你想保留de_allocate原样。

You could test it with

你可以测试一下

#include <stdio.h>
#include <stdlib.h>

int allocate(int **array, unsigned int rows, unsigned int columns){
  int i;
  for (i = 0; i < rows; i++)
  {
    array[i] = malloc(columns * sizeof (int));
  }

  /* Code fo fill the array*/
  return 1;
}

void de_allocate(int **v, unsigned int rows) {
  int i;
  for (i = 0; i < rows; i++)
  {
    free(v[i]);
  }
  free(v);
}

int main(int argc, char **argv) 
{
    int rows, columns;
    int temp = 0;
    rows = atoi(argv[1]);
    columns = atoi(argv[2]);

    int **ipp = malloc(sizeof(int*)*rows);

    allocate(ipp, rows, columns);

    for (int i=0; i<rows; i++)
        for (int j=0; j<columns; j++)
            ipp[i][j] = temp++;

    for (int i=0; i<rows; i++)
        for (int j=0; j<columns; j++)
            printf("ipp[%d][%d] = %d\n", i, j, ipp[i][j]);

    de_allocate(ipp,rows);
    return 0;
}

#2


1  

void de_allocate(int **v, unsigned int rows) {
  int i;
  for (i = 0; i < rows; i++) {
    free(v[i]);
  }
  free(v);
-----^----
here you are attempting to free a variable for which you didn't dynamically allocate memory in the first place

}