2

This code should multiply matrix A with matrix B and calculate matrix C. matA 3x2, matB 2x3 therefore matC should be `3x3. However, when I print matC, the 3rd row contains garbage values. I don't know where the mistake is — if I do something wrong with the pointers or maybe the calculate part is wrong.

This is what I see when printing it.

x x 2489458943
x x 2489587641
x x 2489745734

Or something like that. I don't know why sometimes it works. I mean it prints the correct values, but most of the time this is what I see.

#include <stdio.h>

int main() {
    int x = 0,y = 0, z = 0;
    int i,j,k;
    int **matA;
    int **matB;
    int **matC;
    matA = (int**)malloc(sizeof(int)*3); // making the matrixes by malloc functions
    matB = (int**)malloc(sizeof(int)*2);
    matC = (int**)malloc(sizeof(int)*3);
    for(i = 0; i < 3; i ++)
    {
        matA[i] = (int*)malloc(sizeof(int)*2); // matA 3x2
    }
    for(i = 0; i < 2; i++)
    {
        matB[i] = (int*)malloc(sizeof(int)*3); // matB 2x3
    }
    for(i = 0; i < 3; i++)
    {
        matC[i] = (int*)malloc(sizeof(int)*3); // the calculated mat 3x3
    }
    for(i = 0; i < 3; i++)
    {
        printf("please eneter line number: %i",i+1); //putting the values by scanf function in matA 
        printf(" seperated with ','\n");
        scanf("%d,%d",&x,&y);
        matA[i][0] = x;
        matA[i][1] = y;
    }
    for(i = 0; i < 2; i++)
    {
        printf("please eneter line number: %i",i+1);// putting the values in matB
        printf(" of the second mat seperated with ','");
        scanf("%i,%i,%i", &x, &y, &z);
        matB[i][0] = x;
        matB[i][1] = y;
        matB[i][2] = z;
    }
    for(i = 0; i < 3; i++) // multiple the matrixes by 3 loops
    {
        for(j = 0; j < 3; j++)
        {
            for(k = 0; k < 2; k++)
            {
                matC[j][i] += matA[j][k]*matB[k][i];
            }
        }
    }
    for(i = 0; i < 3; i ++)// just printing to check if the matrix correct
    {
        for(j = 0; j < 3; j++)
        {
            printf("%i ",(int**)matC[i][j]);

        }
        printf("\n");
    }


    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

1

You're not allocating enough space in your matrixes:

matA = (int**)malloc(sizeof(int)*3);
matB = (int**)malloc(sizeof(int)*2);
matC = (int**)malloc(sizeof(int)*3);

You're allocating space for an array int but you need an array of int *. Most likely, pointers are larger than integers on your system which means your arrays aren't large enough to hold what you want and you run of the end of the array. Doing so invokes undefined behavior.

Allocate space for arrays of int *. Also, don't cast the return value of malloc:

matA = malloc(sizeof(int *)*3);
matB = malloc(sizeof(int *)*2);
matC = malloc(sizeof(int *)*3);

You're also adding to elements of matC without initializing them. You should set them to 0 before doing so:

        matC[j][i] = 0;
        for(k = 0; k < 2; k++)
        {
            matC[j][i] += matA[j][k]*matB[k][i];
        }

You also don't need a cast here:

printf("%i ",(int**)matC[i][j]);

Since each matC[i][j] has type int and you're printing an int. This also invokes undefined behavior because the type of the expression doesn't match the type for the format specifier.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • most systems these days are **64-bit** so **pointers** to memory addresses will be **8 bytes**. But the `sizeof( int )` will be 4 bytes along with `sizeof( float )`. `long int` and `double` will be 8 bytes. – ron Nov 06 '18 at 20:35
0

You never initialize matC and then use matC +=

The malloc function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. Try calloc instead.

Also, you have wrong int** initialization, should allocate pointer size sizeof(int*), not just sizeof(int):

matA = (int**)calloc(3, sizeof(int*));
matB = (int**)calloc(2, sizeof(int*));
matC = (int**)calloc(3, sizeof(int*));
Kozyr
  • 201
  • 1
  • 7