0
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int rows = 1000, columns = 1000;
    int a[rows][columns], b[rows][columns], mul[rows][columns], i, j, k;
    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= columns; j++)
        {
            a[i][j] = rand() % 10;
            b[i][j] = rand() % 10;
        }
    }
    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= columns; j++)
        {
            mul[i][j] = 0;
            for (k = 0; k < columns; k++)
            {
                mul[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= columns; j++)
        {
            printf("%d\t", mul[i][j]);
        }
        printf("\n");
    }
    return 0;
}

I try to create a matrix multiply size 1000*1000 but the output is "zsh: segmentation fault ./a.out" I want to know what the problem

Ps. Thankyou

mujigae
  • 95
  • 1
  • 5
  • 5
    At least in one place: `for (i = 1; i <= rows; i++)`. C arrays start at offset 0, not 1. – Jeff Holt Aug 05 '21 at 04:32
  • 4
    you are going out of bounds. Indices in C are zero-based. change your loops : `i = 0; i < rows ; i++` Do the same for columns. – Raildex Aug 05 '21 at 04:32
  • 1
    C arrays (and matrixes) are zero based, indexed 0-999, not 1-1000. – Joachim Isaksson Aug 05 '21 at 04:32
  • 4
    as an additional comment: you are reserving 3000000 elements on the automatic storage. in most implementation, it has very limited storage. you may run into a stack overflow. – Raildex Aug 05 '21 at 04:34
  • 4
    Note that you're allocating the arrays on the stack*. which is a very limited resources. You should be using `malloc` to allocate them on the heal instead. – ikegami Aug 05 '21 at 04:34
  • 3
    Yep, 12 MB of memory is way too much for stack locals. Learn about malloc() – Lee Daniel Crocker Aug 05 '21 at 04:36
  • I changed my loops start 0 but It's the same problem now I will try to use malloc() – mujigae Aug 05 '21 at 05:34
  • Can you focus your question on one of the problems pointed out already in the comment? I propose to either use `int rows = 10, columns = 10;`, which will focus on the problem of accessing beyond arrays. Or change to `int a[rows+1][columns+1], b[rows+1][columns+1], mul[rows+1][columns+1], i, j, k;`, which would focus on the problem of size on the stack. Understanding 0 indexing arrays in C is highly recomended, but strictly speaking can be worked around. If you want to focus on this please use the small array variant. I only propose this because answers currently need to tackle >=3 problems. – Yunnosch Aug 05 '21 at 07:04

1 Answers1

-1

As mentioned in the comments, C uses 0-based indexing and you might be using too much space on the stack. The solution is to create the array on the heap. Here is how you can use malloc for that. C uses, as first element of arrays, the indexes couple (0,0) not (1,1).

#include <stdio.h>
#include <stdlib.h>
#define ROWS 1000
#define COLS 1000

#define ARRAY(m,n) [n+m*COLS]

int main()
{
  
    int * arr = malloc(COLS*ROWS*sizeof(*arr));

    if (arr) {
        // your array operations go here
        free(arr);
    } else {
        perror("malloc: ");
        return 1;
    }

    return 0;
}
HiEd
  • 160
  • 3
  • 1
    That's fine as a demonstration of `malloc()`, but you haven't answered the OP's question: *...the output is "zsh: segmentation fault ./a.out" I want to know what the problem* – Caleb Aug 05 '21 at 06:36