Segmentation Fault, large arrays Segmentation Fault, large arrays c c

Segmentation Fault, large arrays


You are overflowing the stack. 2 * 1024 * 1024 * sizeof(int) is a lot for most systems.

The simplest solution would be to make the arrays static.

static int a[N][N];static int b[N][N];

Other methods:

  • Make the arrays global (this is essentially the same as the above)
  • Use malloc in a loop and of course remember to free

    int **a = malloc(N * sizeof *a);for (i = 0; i < N; i++)    a[i] = malloc(N * sizeof *a[i]);