如何故意引发不使用递归一个StackOverflowException?递归、StackOverflowException

2023-09-04 09:07:17 作者:没有走过我走过的路,就不要对我指指点点。下面小编推荐一些比较

有人告诉我,每一个方法都有一个堆栈1MB的大小。 所以,我认为在初始化一种方法256的整数值会导致StackOverflowException。我想,在code,但没有异常被抛出。

I was told that every method has a stack the size of 1mb. So I assumed that initializing 256 integer values in one method will cause a StackOverflowException. I tried that in code, but no exception was thrown.

那么,如何故意引发StackOverflowException不使用递归?

So, how to deliberately trigger a StackOverflowException without using recursion?

推荐答案

我会添加另一种方法: - )

I'll add another method :-)

unsafe struct FixedBufferExample
{
    public fixed byte Buffer[128 * 1024]; // This is a fixed buffer.
}

现在这个结构是128KB :-)如果声明类型的 FixedBufferExample 它应该使用128KB(即不使用产量或异步的方法)的局部变量的叠加。您可以使用您的堆栈很快。

Now this structure is 128kb :-) If you declare a local variable (of a method that doesn't use yield or async) of type FixedBufferExample it should use 128kb of stack. You can use up your stack quite quickly.