Segmentation fault in C
c, gcc, segmentation-fault, stack-overflow
Solution
The problem is that you are trying to allocate too much memory in the automatic store (AKA "on the stack"). When you comment out the cycle, the compiler optimizes out the allocation along with the now-unused variables, so you do not get a crash.
You need to change the allocation to either static or dynamic memory (AKA "the heap") to fix this problem. Since the issue is inside `main`, making the arrays `static` would be an appropriate choice.
int main()
{
static int vert[1001][1001];
static int hor[1001][1001];
static int dudiag[1416][1416];
static int uddiag[1416][1416];
...
}
In functions other than `main` you could make these arrays dynamic, allocate them using `malloc`/`calloc`, and then `free` them once your program has finished using them.
Problem
I need to fill 2-d array with 0s. But compiled program falls with this error. What's wrong? ``` int main() { int vert[1001][1001]; int hor[1001][1001]; int dudiag[1416][1416]; int uddiag[1416][1416]; int n, k, m; int row, col; int i, j; int answer = 0; for(i = 0; i <= 1000; i++){ for(j = 0; j <= 1000; j++){ vert[i][j] = 0; hor[i][j] = 0; } } ... } ``` When cycle is commented out, it works properly.