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. The way that just allocates new memory to 'transposed' also works okay on web, but on visual basic 2017 it gives access violation error. What is wrong??
When I allocate memory in a defined function, should I free it in the main function? as with 'matrix'(the one that I allocated in main function)? Did I do it correctly?
# 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");
}
}