-2

I have a serious problame.

at the line **c = (int*)malloc(size1 * sizeof(int*)); the compiler gives me this error which I don't really know what it says.

Unhandled exception thrown: read access violation.
c was nullptr. occurred

I don't know what I'm doing wrong.. I initialize every pointer like this.

void BuildMatrix(int ***, int, int);
void FreeMatrix(int ***, int);
void PrintMatrix(int **, int, int);
int **MultiplyMatrixes(int **, int**, int, int, int);

int main() {
    int **matrix1 = NULL, **matrix2 = NULL, **matrix3 = NULL;
    int * newCol = NULL;
    int size1, size2, size3, newRow;

    printf("-How many rows in the first matrix?: ");
    scanf("%d", &size1);
    printf("-How many columns in the first matrix and second?[size2, size3]: ");
    scanf("%d %d", &size2, &size3);  /*size2 = rows of matrix2.*/

    /*Build both matrices*/
    printf("-First matrix input.\n");
    BuildMatrix(&matrix1, size1, size2);
    PrintMatrix(matrix1, size1, size2);
    printf("-Second matrix input.\n");
    BuildMatrix(&matrix2, size2, size3);
    PrintMatrix(matrix2, size2, size3);

    /*Combine the 2 matrices to a new matrix*/
    matrix3 = MultiplyMatrixes(matrix1, matrix2, size1, size2, size3);
    FreeMatrix(&matrix2, size2); //Free the second matrix

    printf("\n-Multiplied matrix: \n");
    PrintMatrix(matrix3, size1, size3);

    FreeMatrix(&matrix3, size1);
    FreeMatrix(&matrix1, size1);
}
void BuildMatrix(int *** pMat, int row, int col) {
    int i, j;
    (*pMat) = (int **)malloc(row * sizeof(int*));
    if (*pMat == NULL) {
        free(pMat);
        printf("*Not enough RAM.\nTerminating.\n");
        exit(1);
    }
    for (i = 0; i < row; i++) {
        (*pMat)[i] = (int *)malloc(col * sizeof(int*));
        if ((*pMat)[i] == NULL) {
            printf("*Not enough RAM.\nTerminating.\n");
            FreeMatrix(pMat, row);
            exit(1);
        }
        for (j = 0; j < col; j++) {
            printf("-Enter %d element in %d row: ", j + 1, i + 1);
            scanf("%d", &(*pMat)[i][j]);
        }
        printf("\n");
    }
    //FreeMatrix(pMat, row);
}
void PrintMatrix(int ** pMat, int row, int col) {
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            printf("%d ", pMat[i][j]);
        }
        printf("\n");
    }
}
int** MultiplyMatrixes(int ** a, int ** b, int size1, int size2, int size3) {
    int i, j, k, **c = NULL;
    **c = (int*)malloc(size1 * sizeof(int*));
    if (c == NULL) {
        free(*c);
        printf("*Not enough RAM.\nTerminating.\n");
        exit(1);
    }
    for (i = 0; i < size1; i++) {

        for (j = 0; j < size3; j++) {
            c[i] = (int *)malloc(size3 * sizeof(int));
            if (c[i] == NULL) {
                printf("*Not enough RAM.\nTerminating.\n");
                FreeMatrix(&c, size1);
                exit(1);
            }
            for (k = 0; k < size2; k++) {
                c[i][j] += (a[i][k] * b[k][j]);
            }
        }
    }
    return c;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    You really want to get out of the habit of stubbing that "Not enough RAM" code into every single allocation call. This is going to clutter up your code with an extraordinary number of these things. If you're concerned about allocation failures write a macro that does that so you have a single place to test for failures, not dozens, hundreds, potentially thousands of independent instances of that. – tadman Jan 02 '18 at 19:56
  • 1
    Another thing to consider from a simplicity and performance perspective is to store the matrix data as a one dimensional array and emulate the other dimensions. A 16 element array is far more efficient than a 4-element pointer array plus an additional 4 times 4 element arrays. The second tier elements can be allocated all over the place which is going to be punishing from a cache perspective. – tadman Jan 02 '18 at 19:58
  • 2
    This is lots of code to find a small bug. Please take your time to provide an [mcve]. Also, don't cast malloc. You don't need to. – klutt Jan 02 '18 at 20:17
  • Thanks i will use your advices. @klutt,@tadman – Aviel Cohen Jan 02 '18 at 20:22
  • 1
    `c` is a pointer-to-a-pointer-to-int. Since `c` is initialized as null, then `*c` will throw an exception. It's not clear what you're actually trying to do with that allocation. – dwilliss Jan 02 '18 at 20:40
  • thanks @dwilliss but someone already helped me. :) – Aviel Cohen Jan 02 '18 at 20:43

1 Answers1

0
(*pMat)[i] = (int *)malloc(col * sizeof(int*));

will be

(*pMat)[i] = malloc(col * sizeof(int));

You have allocated space for col number of int* where you are reading int-s.

Also

**c = (int*)malloc(size1 * sizeof(int*));

will be

c = malloc(size1 * sizeof(int*));

Otherwise you were trying to dereference NULL value which triggered the error you got.

Also the loop will be

for (i = 0; i < size1; i++) {
    c[i] = malloc(size3 * sizeof(int));
    if (c[i] == NULL) {
        printf("*Not enough RAM.\nTerminating.\n");
        FreeMatrix(&c, size1);
        exit(1);
    }

    for (j = 0; j < size3; j++) {
        c[i][j]=0;
        for (k = 0; k < size2; k++) {
            c[i][j] += (a[i][k] * b[k][j]);
        }
    }
}

Don't cast the return value of malloc.

user2736738
  • 30,591
  • 5
  • 42
  • 56