Segmentation error when allocating large arrays on the stack

When I compiled this simple C code, it is great, but after breaking a line, it shows a segmentation error. I do not know what happened to this. Please, help.

#include<stdio.h>
int main()
    {
    int arr[10002][10002];
    int color[10002];
    int neigh;
 // scanf("%d",&neigh);
    return 0;
    }
+5
source share
2 answers

You push the stack arrand color. Presumably, when your call is scanfcommented out, the compiler deletes all these variables, but when it is present, it tries to allocate memory on the stack.

Make the variables global and read in stack memory versus heap memory.

#include<stdio.h>

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

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

, , , . () , malloc.

+5

All Articles