Without using recursion how can a stack overflow exception be thrown?
相关问题
- PHP Recursively File Folder Scan Sorted by Modific
- how to call a C++ dll from C# windows application
- How can I increase the maximum call stack size in
- Ada beginner Stack program
- Catch the same exception twice
相关文章
- Threading in C# , value types and reference types
- Recursively replace keys in an array
- Python: Maximum recursion depth exceeded when prin
- Is recursion good in SQL Server?
- JUnit continue to assert things after expected exc
- How does this list comprehension over the inits of
- Find all subfolders of the Inbox folder using EWS
- How to handle translation of exception message?
Every method call that has not yet returned consumes some stack space. (Methods with more local variables consume more space.) A very deep call stack can result in stack overflow.
Note that on systems with limited memory (mobile devices and such) you don't have much stack space and will run out sooner.
If you call enough methods, a stack overflow can occur anytime. Although, if you get stack overflow errors without using recursion, you may want to rethink how you're doing things. It's just so easy with recursion because in an infinite loop, you call a ton of methods.
The following applies to Windows, but most OSs implement this in a similar fashion.
The short answer is: if you touch the last guard page, it will throw.
An exception of type EXCEPTION_STACK_OVERFLOW (C00000FD) is raised when your application touches the bottom page of the stack, that is marked a PAGE_GUARD protection flag, and there is no room to grow the stack (commit one more page), see How to trap stack overflow in a Visual C++ application.
The typical case when this happens is when the stack has grown as the result of many function frames on the stack (ie. out of control recursion), as the result of fewer frames but very large frame sizes (functions with a very large local scoped object) or by explicitly allocating from the stack with
_alloca
.Another way to cause the exception is to simply intentionally touch the guard page, eg. by dereferencing a pointer that points into that page. This can happen due to a variable initializion bug.
Stack overflows can occur on valid execution paths if the input causes a very deep nesting level. For instance see Stack overflow occurs when you run a query that contains a large number of arguments inside an IN or a NOT IN clause in SQL Server.
Easiest way to make a StackOverflowException is the following:
Since no one else has mentioned it:
You might do this when testing or doing fault-injection.