defining large object in stack may cause stack overflow

A lot of problems require you to define a huge two-dimensional array of huge size, say 10000 by 10000. What you would typically do is:

1
2
3
4
5
int main() {
int f[10000][10000] = {0};
// ...
return 0;
}

Because the amount of space in a stack is very limited, doing so will cause stack overflow immediately, depending on your computer:

1
segmentation fault

The correct way to define huge objects is:

1
2
3
4
5
int f[10000][10000] = {0};
int main() {
// ...
return 0;
}

This words, because now the object is in global data section which has far more space than stack.

Or better, you would like to define big things in the heap instead of in a stack frame:

1
2
3
int** a = new int*[10000];
for (int i = 0; i < 10000; ++i)
a[i] = new int[10000];