没有重新分配内存到'转置'我尝试重新分配'矩阵'https://codepad.remoteinterview.io/WGIBSKENXV当我在网上运行它它工作正常但在visual basic 2017上,它给了我访问冲突错误在双循环部分。 刚刚为“转置”分配新内存的方式在网络上也可以正常工作,但在visual basic 2017上它会出现访问冲突错误。 有什么不对?
当我在定义的函数中分配内存时,我应该在main函数中释放它吗?和'矩阵'(我在main函数中分配的那个)一样?我做得对吗?
# 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, i;
int **matrix;
printf("Number of Rows : ");
scanf("%d", &rows);
printf("Number of Cols : ");
scanf("%d", &cols);
matrix = (int **)malloc(rows*sizeof(int*));
for(i = 0; i < rows; i++){
matrix[i] = (int*)malloc(cols*sizeof(int));
}
srand(2016);
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);
i = 0;
while(matrix[i] != 0){
free(matrix[i]);
++i;
}
free(matrix);
/*
i = 0;
while(transposed[i] != 0){
free(transposed[i]);
++i;
}
free(transposed);
*/
return 0;
}
//When I allocate memory in a defined function, should I free it in the main function? as with 'matrix'? Did I do it correctly?
int** transpose (int **matrix, int rows, int cols){
int i ,j;
if( rows < cols ){
for(i = 0; i < rows; i++){
matrix[i] = (int*)realloc(matrix[i], rows*sizeof(int));
}
}
else if(cols < rows){
matrix = (int **)realloc(matrix, cols*sizeof(int*));
}
for (i = 0; i < rows; i++) {
for (j = i +1 ; j < cols ; j++) {
tmp= *(*(matrix + i) + j);
*(*(matrix + i) + j) = *(*(matrix + j) + i);
*(*(matrix + j) + i) = tmp;
}
}
return matrix;
// Without newly allocating memory to 'transposed' I tried to realloc on 'matrix'
https://codepad.remoteinterview.io/WGIBSKENXV when I run it on web it works fine but on visual basic 2017, it gives me access violation error at double for loop part.
/*
int ** transposed;
transposed = (int **)malloc(cols*sizeof(int*));
for(i = 0; i < cols; i++){
transposed[i] = (int*)malloc(rows*sizeof(int));
}
for (i = 0; i < rows; i++) {
for (j = 0 ; j < cols; j++) {
*(*(transposed + j )+ i )= *(*(matrix + i) + j);
*(*(transposed + i) + j) = *(*(matrix + j) + i);
}
}
return transposed;
*/
// Just allocating new memory to 'transposed' works okay on web https://codepad.remoteinterview.io/WGIBSKENXV, but on visual basic 2017 it gives access violation error. When I allocate memory in a defined function, should I free it in the main function? as with 'matrix'? Did I do it correctly?
}
void printMatrix(int **matrix, int m, int n){
int i, j;
for( i = 0; i < m; i++ ){
for( j = 0; j < n; j++ ){
printf("%4d", *(*(matrix + i )+j) );
}
printf("\n");
}
}
答案 0 :(得分:1)
else if(cols < rows){
matrix = (int **)realloc(matrix, cols*sizeof(int*));
}
在这种情况下,您将使用较小的行重新分配矩阵,但是后面的循环仍会访问它,直到rows
for (i = 0; i < rows; i++) {
for (j = i +1 ; j < cols ; j++) {
tmp= *(*(matrix + i) + j);
*(*(matrix + i) + j) = *(*(matrix + j) + i);
*(*(matrix + j) + i) = tmp;
}
}
*(*(matrix + i) + j);
当我&gt; cols将导致访问无效内存,因为矩阵被重新分配到cols
个地址