Execution stops automatically in c when i use malloc()

247 Views Asked by At

I am trying to develop strassen's matrix multiplication program , Program just stops it's execution from the first time malloc is called in matrix_mul() function Can anyone explain what the issue is??

I tried to debug the code and added printf() right before the malloc function called in for loop and also add the printf() after the for loop.

I am trying to do it recursively. code is not fully completed but i was just checking that everything else is working properly but i am having this issue.

  #include <stdio.h>
    #include<stdlib.h>
    #define pfm(a,n) for (int i = 0; i < n; ++i){printf("\nrow %d:",i);for (int j = 0; j < n; ++j)printf("%d",a[i][j]);}
    #define sfm(a,n) for (int i = 0; i < n; ++i)for (int j = 0; j < n; ++j)scanf("%d",&a[i][j]);
    #define debug(x) printf("debugging x = %d",x);
    
    
    int **matrix_add(int n, int **mat1, int **mat2) {
        int **x;
        for (int i = 0; i < n; ++i)
        {
            x[i] = malloc(sizeof(int *) * n);
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                x[i][j] = mat1[i][j] + mat2[i][j];
            }
        }
        pfm(x, n)
        return x;
    }
    
    int  **matrix_mul(int n, int matrix1[n][n] , int matrix2[n][n]) {
    
        int a[n / 2][n / 2], b[n / 2][n / 2], c[n / 2][n / 2], d[n / 2][n / 2];
        int e[n / 2][n / 2], f[n / 2][n / 2], g[n / 2][n / 2], h[n / 2][n / 2];
        int **new;
        //divide matrix into 4 matrix for both matrix total 8 matrix a to h
        printf("Assigning memory.....\n");
        for (int i = 0; i < n; ++i)
        {
            new[i] = malloc(sizeof(int *) * n);
        }
        printf("Done\nn=%d\n", n );
    
        if (n == 1) {
            new[0][0] = matrix1[0][0] + matrix2[0][0];
            printf("Base");
            return new;
        }
    
        for (int i = 0; i < n / 2; ++i)
        {
            for (int j = 0; j < n / 2; ++j)
            {
                //matrix1
                a[i][j] = matrix1[i][j];
                b[i][j] = matrix1[i][j + n / 2];
                c[i][j] = matrix1[i + n / 2][j];
                d[i][j] = matrix1[i + n / 2][j + n / 2];
    
                //matrix2
                e[i][j] = matrix2[i][j];
                f[i][j] = matrix2[i][j + n / 2];
                g[i][j] = matrix2[i + n / 2][j];
                h[i][j] = matrix2[i + n / 2][j + n / 2];
            }
        }

    for (int i = 0; i < n / 2; ++i)
    {
        for (int j = 0; j < n / 2; ++i)
        {
            new[i][j] = matrix_add(n / 2, matrix_mul(n / 2, a, e), matrix_mul(n / 2, a, e))[i][j];
        }
    }
    pfm(new, n);

    // [a, b] [e, f]
    // [c, d] * [g, h]

    // [ae + bg, af + bh]
    // [ce + dg, cf + dh]
    return new;
}

int main(int argc, char const *argv[])
{
    int n, **result;
    printf("Enter the matrix size:\n");
    scanf("%d", &n);
    int matrix1[n][n], matrix2[n][n];
    sfm(matrix1, n);
    sfm(matrix2, n);

    result = matrix_mul(n, matrix1, matrix2);
    pfm(result, n);

    // pfm(matrix, n);
    return 0;
}
0

There are 0 best solutions below