Arrays in automatic storage are allocated on the stack. Stack size is limited. If there is not enough stack space to allocate automatic variables, an exception is thrown.
If you need large arrays, use static or dynamic allocation instead.
For static distribution, move declarations to external main()
.
For dynamic allocation, use the following code:
char *w = new char[1000001], *temp = new char[1000001]; // Work with w and temp as usual, then delete[] w; delete[] temp;
Finally, consider using standard containers instead of simple arrays: std::array
is the best array if you do not need to resize (it is allocated on the stack and will not solve this problem); std::string
also a good candidate for replacing char
arrays.
source share