Trying to understand the problems in my code

0

I'm trying to do a function that does the following:

• Receives 3 integers from the user: size1, size2, size3.

• Creates a size1 * size2 matrix and a size2 * size3 matrix.

• Multiplies the 2 matrices.

• Prints the result matrix.

• Free all dynamic memory.

after edit and I got an error after I write two matrixs that says: Exception thrown at 0x0FA6AC1D (ucrtbased.dll) in Project8.exe: 0xC0000005: Access violation reading location 0xCCCCCCC4.

The code is:

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>

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] = 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 FreeMatrix(int*** matrix, int row)
{
    for (int i = 0; i < row; i++)
    {
        free((matrix)[i]);
    }
    free(matrix);
}

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 = malloc(size1 * sizeof(int*));
    if (c == NULL) 
    {
        free(*c);
        printf("*Not enough RAM.\nTerminating.\n");
        exit(1);
    }

    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]);
            }
        }
    }
}

The error list: enter image description here

  1. List item
Severity    Code    Description Project File    Line    Suppression State
Warning C4101   'newRow': unreferenced local variable   Project8    C:\Users\Shai\source\repos\Project8\Project8\Source1.c  13  
Warning C6011   Dereferencing NULL pointer 'c'.     Project8    C:\USERS\SHAI\SOURCE\REPOS\PROJECT8\PROJECT8\SOURCE1.C  96  
Warning C4715   'MultiplyMatrixes': not all control paths return a value    Project8    C:\Users\Shai\source\repos\Project8\Project8\Source1.c  119 

c
visual-studio
matrix
dynamic
error-handling
asked on Stack Overflow May 17, 2020 by Shay Fletcher • edited May 18, 2020 by Shay Fletcher

1 Answer

0

You're missing "#include <stdio.h>", so printf and scanf are unrecognized. when declaring a function like you did at the top of the code you need to give your variables names just like the signature when you made the function itself:

void BuildMatrix(int*** pMat, int row, int col);
void PrintMatrix(int** pMat, int row, int col);
int** MultiplyMatrixes(int** a, int** b, int size1, int size2, int size3);
int** MultiplyMatrixes(int** a, int** b, int size1, int size2, int size3);
void FreeMatrix(int***pMat, int idk);

in the code you've given there's no FreeMatrix by the way make sure you have that function in your code

answered on Stack Overflow May 17, 2020 by ofri harel

User contributions licensed under CC BY-SA 3.0