Segmentation fault when allocating large arrays on the stack

You’re blowing the stack with arr and color. Presumably when your call to scanf is commented out the compiler optimises all these variables away, but when it’s present it attempts to allocate memory on the stack.

Make the variables global, and read up on stack memory vs heap memory.

#include<stdio.h>

int arr[10002][10002];
int color[10002];

int main()
{
    int neigh;
    scanf("%d",&neigh);
    return 0;
}

Leave a Comment