Malika Sadullaeva
Malika Sadullaeva

Reputation: 19

Matrix Multiplication C language

So, I have problem with matrix multiplication. I have to store the values of a matrix in a file and after that multiply them. The problem occurs when I try to multiply 900x900 matrix: Segmentation fault (core dumped), but 800x800 works perfectly). there is part of my code: create file for storing:

FILE *A, *B;
int num = atoi(argv[1]);
float a[num][num];
float b[num][num];
A = fopen(argv[2],"r");
B = fopen(argv[3],"r");
for (int i = 0; i < num; ++i)
{
    for (int j = 0; j < num; ++j)
    {
        fscanf(A,"%f",&a[i][j]);
    }
}
for (int i = 0; i < num; ++i)
{
    for (int j = 0; j < num; ++j)
    {
        fscanf(B,"%f",&b[i][j]);
    }
}

So i didn't write function for matrix multiplication because it works

Upvotes: 0

Views: 152

Answers (1)

Clifford
Clifford

Reputation: 93456

Your two float variable-length arrays occupy 2*9002*4 bytes - that is a little over 6Mb. VLAs are typically created on the stack, the size of which will vary between systems and processes, but on a modern desktop system is typically perhaps 2 to 8 Mb.

Creating an array that large on the stack is somewhat unreasonable, and failure unsurprising.

Upvotes: 3

Related Questions