为什么realloc会把我扔出去;分段故障(堆芯倾倒)“;

为什么realloc会把我扔出去;分段故障(堆芯倾倒)“;,c,C,我对C很陌生,这是学校的作业。 所以,我的任务是转置一个给定的矩阵 我目前的职能如下: void matrixTranspose(int rows, int cols, int **array) { int temp[rows][cols]; int i, j; for (i = 0; i < rows; i++) { for(j = 0; j < cols; j++) { temp[i][j] = array[i][

我对C很陌生,这是学校的作业。 所以,我的任务是转置一个给定的矩阵

我目前的职能如下:

void matrixTranspose(int rows, int cols, int **array) {
    int temp[rows][cols];
    int i, j;

    for (i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            temp[i][j] = array[i][j];
        }
    }

    array = realloc(array, cols * sizeof(int *));

    for (i = 0; i < cols; i++) {
        array[i] = realloc(array[i], rows * sizeof(int));
    }

    for (i = 0; i < cols; i++) {
        for(j = 0; j < rows; j++) {
            array[i][j] = temp[j][i];
        }
    }
 }
void matrixTranspose(整数行、整数列、整数**数组){
int temp[行][列];
int i,j;
对于(i=0;i
如果我为列和行引入相等的值,或者如果行的值大于列的值,那么它可以正常工作,但是由于某种原因,当行的值小于列的值时,它不起作用。(抛出“分段错误(堆芯转储)”错误)

我的主要观点如下:

int main() {
    int **mat;
    int cols, rows;
    int i, j;

   printf("Enter number of rows\n");
    scanf("%d", &rows);
    printf("Enter number of columns\n");
    scanf("%d", &cols);

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

   for (i = 0; i < rows; i++) {
       mat[i] = (int *) malloc (sizeof(int) * cols);
   }

   for (i = 0; i < rows; i++) {
       for(j = 0; j < cols; j++) {
           mat[i][j] = rand() % 10;
       }
   }

   printf("\nBefore transpose: \n");
    for (i = 0; i < rows; i++) {
       for(j = 0; j < cols; j++) {
           printf("%d ", mat[i][j]);
       }
       printf("\n");
   }

   matrixTranspose(rows, cols, mat);
   printf("\nAfter transpose: \n");

   for (i = 0; i < cols; i++) {
       for(j = 0; j < rows; j++) {
           printf("%d ", mat[i][j]);
      }
       printf("\n");
   }

 }
int ** matrixTranspose(int rows, int cols, int **array) {
    ...
    return array;
}
mat = matrixTranspose(rows, cols, mat);
intmain(){
int**mat;
int列,行;
int i,j;
printf(“输入行数\n”);
scanf(“%d”,行和行);
printf(“输入列数\n”);
scanf(“%d”、&cols);
mat=(int**)malloc(sizeof(int*)*行);
对于(i=0;i

我希望我的解释正确,对不起,我的英语不是我的第一语言。谢谢

当您在
矩阵传输
中修改
数组
时,您正在更改一个局部变量。该更改在调用函数中不可见,因此
main
中的
mat
不再指向有效内存

您需要更改函数以接受
int**
的地址,并根据需要取消对它的引用

void matrixTranspose(int rows, int cols, int ***array) {
    int temp[rows][cols];
    int i, j;

    for (i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            temp[i][j] = (*array)[i][j];
        }
    }

    *array = realloc(*array, cols * sizeof(int *));
    if (!*array) {
        perror("realloc failed");
        exit(1);
    }

    int min = rows < cols ? rows : cols;
    for (i = 0; i < min; i++) {
        (*array)[i] = realloc((*array)[i], rows * sizeof(int));
        if (!(*array)[i]) {
            perror("realloc failed");
            exit(1);
        }
    }
    if (rows > cols) {
        for (i = min; i < rows; i++) {
            free((*array)[i]);
        }
    } else if (cols > rows) {
        for (i = min; i < cols; i++) {
            (*array)[i] = malloc(rows * sizeof(int));
            if (!(*array)[i]) {
                perror("malloc failed");
                exit(1);
            }
        }
    }

    for (i = 0; i < cols; i++) {
        for(j = 0; j < rows; j++) {
            (*array)[i][j] = temp[j][i];
        }
    }
 }

您可以这样更改转置函数:

int main() {
    int **mat;
    int cols, rows;
    int i, j;

   printf("Enter number of rows\n");
    scanf("%d", &rows);
    printf("Enter number of columns\n");
    scanf("%d", &cols);

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

   for (i = 0; i < rows; i++) {
       mat[i] = (int *) malloc (sizeof(int) * cols);
   }

   for (i = 0; i < rows; i++) {
       for(j = 0; j < cols; j++) {
           mat[i][j] = rand() % 10;
       }
   }

   printf("\nBefore transpose: \n");
    for (i = 0; i < rows; i++) {
       for(j = 0; j < cols; j++) {
           printf("%d ", mat[i][j]);
       }
       printf("\n");
   }

   matrixTranspose(rows, cols, mat);
   printf("\nAfter transpose: \n");

   for (i = 0; i < cols; i++) {
       for(j = 0; j < rows; j++) {
           printf("%d ", mat[i][j]);
      }
       printf("\n");
   }

 }
int ** matrixTranspose(int rows, int cols, int **array) {
    ...
    return array;
}
mat = matrixTranspose(rows, cols, mat);
然后在
main
中这样称呼它:

int main() {
    int **mat;
    int cols, rows;
    int i, j;

   printf("Enter number of rows\n");
    scanf("%d", &rows);
    printf("Enter number of columns\n");
    scanf("%d", &cols);

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

   for (i = 0; i < rows; i++) {
       mat[i] = (int *) malloc (sizeof(int) * cols);
   }

   for (i = 0; i < rows; i++) {
       for(j = 0; j < cols; j++) {
           mat[i][j] = rand() % 10;
       }
   }

   printf("\nBefore transpose: \n");
    for (i = 0; i < rows; i++) {
       for(j = 0; j < cols; j++) {
           printf("%d ", mat[i][j]);
       }
       printf("\n");
   }

   matrixTranspose(rows, cols, mat);
   printf("\nAfter transpose: \n");

   for (i = 0; i < cols; i++) {
       for(j = 0; j < rows; j++) {
           printf("%d ", mat[i][j]);
      }
       printf("\n");
   }

 }
int ** matrixTranspose(int rows, int cols, int **array) {
    ...
    return array;
}
mat = matrixTranspose(rows, cols, mat);
除此之外,我推荐这些唐人街。我已将参数更改为
sizeof
作为实际变量,而不是类型

array = realloc(array, cols * sizeof(*array));

for (i = 0; i < cols; i++) {
    array[i] = realloc(array[i], rows * sizeof(*array[0]));
}
array=realloc(数组,cols*sizeof(*array));
对于(i=0;i

mat=(int**)malloc(sizeof(*mat)*行);
对于(i=0;i
数组是按值传递的(即不传递指向矩阵的指针)。然而你改变了它

这只是局部的变化。而且

(i=0;i{ 数组[i]=realloc(数组[i],行*sizeof(int)); } 如果
此代码段将尝试为
数组[i]
重新分配内存,其中
i>行-1
。这意味着取消分配
array[i]
指向的内存从未分配过,您也不知道它指向哪里

我的任务是转换给定的矩阵

关键问题:代码通过值传递指针,matrixTranspose()需要接收其地址才能修改它。别人回答得很好,但变化不大


<>但是考虑一个更大的变化。不要修改原始矩阵,制作转置副本和自由矩阵辅助函数

int **matrixTranspose_copy(int rows, int cols, const int **array) {
  int **transpose = malloc(sizeof *transpose * cols);
  for (int r = 0; r < cols; r++) {
    transpose[r] = malloc(sizeof *transpose[r] * rows);
    for(int c = 0; c < rows; c++) {
      transpose[r][c] = array[c][r];
      }
    }
  }
  return transpose;
}

void matrixFree(int rows, const int **array) {
  for (int r = 0; r < rows; r++) {
    free(array[r]);
    }
  free(array);
}

void matrixTranspose_inplace(int rows, int cols, int ***array) {
  int **original = *array;
  *array = matrixTranspose_copy(rows, cols, original);
  matrixFree(original);
}
int**matrixTranspose\u复制(int行、int列、常量int**数组){
int**transpose=malloc(sizeof*transpose*cols);
for(int r=0;r
array
是按值传递的,因此在返回
matrixTranspose
时不会得到新的分配。您假定
realloc
将始终成功。如果
realloc
失败,它将返回
NULL
,但也不会释放传入内存中的数据。因此,您的调用将泄漏原始分配。如果
realloc
使
数组的行数减少,则先前多余的行已泄漏。