Without using recursion how can a stack overflow e

2019-02-22 07:34发布

Without using recursion how can a stack overflow exception be thrown?

9条回答
闹够了就滚
2楼-- · 2019-02-22 08:17
int main()
{
  //something on the stack
  int foo = 0;
  for (
    //pointer to an address on the stack
    int* p = &foo;
    //forever
    ;
    //ever lower on the stack (assuming that the stack grows downwards)
    --p)
  {
    //write to the stack
    *p = 42;
  }
}
查看更多
唯我独甜
3楼-- · 2019-02-22 08:20

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.

查看更多
Lonely孤独者°
4楼-- · 2019-02-22 08:21

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.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-02-22 08:21

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.

查看更多
一夜七次
6楼-- · 2019-02-22 08:22

Easiest way to make a StackOverflowException is the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            SomeClass instance = new SomeClass();
            string name = instance.Name;
        }
    }

    public class SomeClass
    {
        public string Name
        {
            get
            {
                return Name;
            }
        }
    }
}
查看更多
地球回转人心会变
7楼-- · 2019-02-22 08:25

Since no one else has mentioned it:

throw new System.StackOverflowException();

You might do this when testing or doing fault-injection.

查看更多
登录 后发表回答