【发布时间】:2015-07-02 03:53:14
【问题描述】:
我想知道在我们得到堆栈溢出异常之前,我们可以在 c# 中的堆栈中执行多少调用
所以我决定写下面的代码
static void Method2(int Calls)
{
if(!Calls.Equals(0))
Method1(--Calls);//if more calls remain call method1 and reduce counter
}
static void Method1(int Calls)
{
if (!Calls.Equals(0))//if more calls remain call method2 and reduce counter
Method2(--Calls);
}
static void Main(string[] args)
{
var Calls= 42994;//number of calls(stack overflow appears for large number)
Method1(Calls);
}
我的问题是编译器如何决定抛出堆栈溢出异常 这是关于内存限制吗? 一旦我输入 42995,我得到了 stackoverflow,但这个数字不是恒定的,所以这是如何工作的?
【问题讨论】:
-
技术上,编译器不会抛出这个异常,运行时会。
-
嗯...这是怎么回事?@BradleyDotNET
-
堆栈中的空间量是恒定的,而不是函数调用的数量是恒定的。
标签: c# stack-overflow callstack limits