This question is mystifying me for years and considering this site's name, this is the place to ask.
Why do we, programmers, still have this StackOverflow
problem?
Why in every major language does the thread stack memory have to be statically allocated on thread creation?
I will speak in the context of C#/Java, because I use them most, but this is probably a broader problem.
Fixed stack size leads to huge problems:
- There is no way to write a recursive algorithm unless you are absolutely sure that the depth of recursion is tiny. Linear memory complexity of the recursive algorithm is often unacceptable.
- There is no cheap way to start new threads. You have to allocate huge block of memory for stack to account for all the possible uses of the thread.
- Even if you don't use very deep recursion, you always have a risk of running out of stack space for the reason that the stack size is an arbitrary fixed number. Considering that the StackOverflow is usually unrecoverable, this is a big problem in my eyes.
Now, if the stack was resized dynamically, all of the problems above would be much alleviated, because stack overflow would only be possible when there is a memory overflow.
But this is not the case yet. Why? Are there some fundamental limitations of modern CPUs which would make it impossible/inefficient? If you think about the performance hit that reallocations would impose, it should be acceptable because people use structures like ArrayList
all the time without suffering much.
So, the question is, am I missing something and the StackOverflow is not a problem, or am I missing something and there are a lot of languages with dynamic stack, or is there some big reason for this being impossible/hard to implement?
Edit: Some people said that performance would be a large problem, but consider this:
- We leave the compiled code untouched. The stack access stays the same, thus the "usual case" performance stays the same.
- We handle CPU exception which happens when the code tries to access the unallocated memory and launch our "reallocation" routine. Reallocations won't be frequent because <put your usual ArrayList argument here>. Should work on most protected-mode CPUs without loss of performance. No?