Possible Duplicate:
C programming, why does this large array declaration produce a segmentation fault?
Why does this program crash? It works fine for a max of 1e6, and works fine if I don’t set every value to 0.
Doesn’t the program get all of the memory allocated?
int main() {
const int max = 10000000; // 1e7
int end[max];
unsigned int i;
for ( i = 0; i < max; i++ )
end[i] = 0;
}
$ gcc test.c && ./a.out
Segmentation fault (core dumped)
Variable-length arrays are usually allocated on the stack. Stack space is limited; you are probably getting a stack overflow here. You can’t allocate a very large size like that on such memory location. Your array is too big to fit in your program’s stack. A better idea to allocate such size is to allocate on the heap.
The heap is many times larger than the stack.