我正在尝试编写一个使用指针和内存分配来转置矩阵的代码。
当我运行它时,它会冻结在下面给出的程序中产生 rand function
矩阵的部分。
所以我无法检查两个函数(transpose,printMatrix)是否有效。
我做错了什么?
# include <stdio.h>
# include <stdlib.h>
int** transpose (int **matrix, int m, int n);
void printMatrix(int **matrix, int m, int n);
int main (void){
int rows, cols;
int r, c;
int **matrix;
printf("Number of Rows : ");
scanf("%d", &rows);
printf("Number of Cols : ");
scanf("%d", &cols);
matrix = (int **)malloc(rows*sizeof(int*));
matrix[0] = malloc(rows*cols*sizeof(int));
srand(2016);
//error starts from here
for( r = 0; r < rows; r++ ){
for( c = 0; c < cols; c++ ){
*(*(matrix + r)+c) = rand() % 99 + 1;
}
}
printf("Matrix produced with seed number 2016\n");
printMatrix(**matrix, rows, cols);
matrix = transpose(**matrix, rows, cols);
printf("Transposed Matrix\n");
printMatrix(**matrix, rows, cols);
}
int** transpose (int **matrix, int rows, int cols){
int tmp, i ,j;
if( rows < cols ){
matrix[0] = (int *)realloc(matrix[0], rows*rows*sizeof(int));
}
else if(cols < rows){
matrix = (int **)realloc(matrix, cols*sizeof(int*));
}
for (i = 0; i < rows; i++) {
for (j = 0 ; j < cols; j++) {
tmp = *(*(matrix + i) + j);
*(*(matrix + i) + j) = *(*(matrix + j) + i);
*(*(matrix + j) + i) = tmp;
}
}
return matrix;
}
void printMatrix(int **matrix, int m, int n){
int i, j;
for( i = 0; i < m; i++ ){
for( j = 0; j < n; j++ ){
printf("%3d", *(*(matrix + i )+j) );
}
printf("\n");
}
}
答案 0 :(得分:1)
您正在从matrix [0]开始分配连续的行数* cols * sizeof(int):
matrix[0] = malloc(rows*cols*sizeof(int));
我认为分配应该是:
matrix = (int **)malloc(rows*sizeof(int*));
for(int i = 0; i < rows; i++)
matrix[i] = (int*)malloc(cols*sizeof(int));
答案 1 :(得分:1)
您的问题在于以下代码:
matrix = (int **)malloc(rows*sizeof(int*));
matrix[0] = malloc(rows*cols*sizeof(int));
在第一个语句中,您尝试分配指针数组。但是,在第二个语句中,您只将内存分配给一个指针。因此,当您尝试执行*(*(matrix + r)+c)
时,它会访问无效的内存。
你应该这样做:
matrix = (int **)malloc(rows*sizeof(int*));
for (int i=0; i<rows; i++) {
matrix[i] = malloc(cols*sizeof(int));
}
答案 2 :(得分:1)
正如其他答案所述,您的问题在于矩阵的内存分配。但是你的问题并没有就此止步。当你想在函数中使用双指针时,你也传递了矩阵的值。
请参阅更正后的代码:
# include <stdio.h>
# include <stdlib.h>
int** transpose (int **matrix, int m, int n);
void printMatrix(int **matrix, int m, int n);
int main (void){
int rows, cols;
int r, c;
int **matrix;
printf("Number of Rows : ");
scanf("%d", &rows);
printf("Number of Cols : ");
scanf("%d", &cols);
matrix = (int **)malloc(rows*sizeof(int*));
for(int i=0;i<rows;i++)
matrix[i] = malloc(cols*sizeof(int));
srand(2016);
//error starts from here
for( r = 0; r < rows; r++ ){
for( c = 0; c < cols; c++ ){
*(*(matrix + r)+c) = rand() % 99 + 1;
}
}
printf("Matrix produced with seed number 2016\n");
//Changed this, you where doing printMatrix(**matrix, rows, cols);
printMatrix(matrix, rows, cols);
//Changed this, you where doing matrix = transpose(**matrix, rows, cols);
matrix = transpose(matrix, rows, cols);
printf("Transposed Matrix\n");
//Changed this, you where doing printMatrix(**matrix, rows, cols);
printMatrix(matrix, rows, cols);
}
int** transpose (int **matrix, int rows, int cols){
int tmp, i ,j;
if( rows < cols ){
matrix[0] = (int *)realloc(matrix[0], rows*rows*sizeof(int));
}
else if(cols < rows){
matrix = (int **)realloc(matrix, cols*sizeof(int*));
}
for (i = 0; i < rows; i++) {
for (j = 0 ; j < cols; j++) {
tmp = *(*(matrix + i) + j);
*(*(matrix + i) + j) = *(*(matrix + j) + i);
*(*(matrix + j) + i) = tmp;
}
}
}
void printMatrix(int **matrix, int m, int n){
int i, j;
for( i = 0; i < m; i++ ){
for( j = 0; j < n; j++ ){
printf("%3d", *(*(matrix + i )+j) );
}
printf("\n");
}
}