Reputation:
When i compile my code for 100000 input array it doesn't give any error. However, when I increase input to 500000 it gives error about stack overflow. I need to increase stack size. How can I do it?
Upvotes: 3
Views: 2336
Reputation: 6404
The answer to the question is to check your compiler's documentation. There's often an option for setting maximum stack size. The default is usually quite small, because well-structured programs have a tree-like call structure, and so the stack take grows logarithmically with program size and complexity. You don't need much stack for a normal program.
As others have said, however, the real answer is probably to move the data off the stack and onto the heap.
Upvotes: 0
Reputation: 234875
Don't rely on large arrays with automatic storage duration. The C++ standard does not mandate a limit on the size of such arrays, but most implementations have a surprisingly small limit compared to the amount of memory that you can allocate with alternative approaches.
In your case, a std::vector<T>
where T
is the element type, would be appropriate.
Upvotes: 2