0

I have a problem with this code. I have to calculate a transposed matrix using dynamic allocation. The code works with 2x2 matrix but when I increase the matrix's size (like 3x3) it fails. Someone can help me?

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

#define LINE 2
#define COLUMN 2


int** createMat(int line,int colum);
void printMat(int **m, int line,int colum);
void assignMat(int **m, int line, int colum);
int** copyMat(int **m, int line, int colum);
int** traspMat(int **m, int line, int colum);

int main(int argc, char **argv){
    int line = LINE;
    int colum = COLUMN;
    int **matrix;
    int **copy_matrix;
    int **trasp_matrix;
    matrix = createMat(line, colum);
    printf("Pre-Assign\n");
    printMat(matrix, line, colum);
    assignMat(matrix, line, colum);
    printf("After-Assign\n");
    printMat(matrix, line, colum);
    copy_matrix = copyMat(matrix, line, colum);
    printf("Matrix Copied\n");
    printMat(copy_matrix, line, colum);
    trasp_matrix = traspMat(matrix, line, colum);
    printf("Transposed matrix\n");
    printMat(trasp_matrix, line, colum);
    free(matrix);
    free(copy_matrix);
    free(trasp_matrix);
    return 0;
}

int** createMat(int line,int colum){
    int **m = (int**)malloc(line * sizeof(int));
    for(int i = 0; i < line; i++){
        m[i] = (int*)malloc(colum * sizeof(int));
    }
    return m;
 }

 void printMat(int **m, int line,int colum){
    for(int i = 0; i < line; i++){
        for(int j = 0; j < colum; j++){
            printf("%d  ", m[i][j]);
        }
        printf("\n");
    }
}

void assignMat(int **m, int line, int colum){
    printf("Inserisci i valori nella matrice (per riga)\n");
    for (int i = 0; i < line; i++){
        for(int j = 0; j < colum; j++){
            printf("> ");
            scanf(" %d", &m[i][j]);
        }
    }
    printf("\n");
}

int** copyMat(int **m, int line, int colum){
    int **new_mat = createMat(line, colum);
    for(int i = 0; i < line; i++){
        for(int j = 0; j < colum; j++){
            new_mat[i][j] = m[i][j];
        }
    }
    return new_mat;
}

int** traspMat(int **m, int line, int colum){
    int **new_m = createMat(line, colum);
    for(int i = 0; i < line; i++){
        for(int j = 0; j < colum; j++){
            new_m[i][j] = m[j][i];
        }
    }
    return new_m;
}
SerPecchia
  • 83
  • 6
  • 2
    Note that `malloc(line * sizeof(int))` allocate `line` number of *integers*, not `line` number of *pointers*. If `sizeof(int) != sizeof(int*)` then you're in big trouble. – Some programmer dude Dec 20 '16 at 16:01
  • I also recommend [this discussion about casting the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Dec 20 '16 at 16:02
  • 1
    Adding on to what @Someprogrammerdude said, it's quite likely that `sizeof(int)` and `sizeof(int*)` are not the same. – Eli Sadoff Dec 20 '16 at 16:03
  • [is-2d-array-a-double-pointer?](http://stackoverflow.com/questions/7586702/is-2d-array-a-double-pointer) – LPs Dec 20 '16 at 16:06
  • 1
    Use `int **m = malloc(line * sizeof(*m));` instead. – RoadRunner Dec 20 '16 at 16:07
  • Thanks @Someprogrammerdude. But why it works for 2x2 matrix? It's a fluke? – SerPecchia Dec 20 '16 at 16:09
  • 1
    *Undefined behavior!* One of the possibilities is that it might *seem* to work. – Some programmer dude Dec 20 '16 at 16:14
  • There is no point in trying to figure out why a particular thing happens when you invoke undefined behavior. By turning on compiler warnings and doing away with the cast of `malloc()`, this sort of error is easy to spot. Also, please consider using more whitespace in your code. – ad absurdum Dec 20 '16 at 16:18
  • A matrix is a 2D array. Your code does not use a 2D array, nor something which can point to one. – too honest for this site Dec 20 '16 at 16:22

1 Answers1

0

Note that malloc(line * sizeof(int)) allocate line number of integers, not line number of pointers. If sizeof(int) != sizeof(int*) then you're in big trouble. – Some programmer dude

Use int **m = malloc(line * sizeof(*m)); instead. – RoadRunner

Armali
  • 18,255
  • 14
  • 57
  • 171