如何故意引发不使用递归一个StackOverflowException? [英] how to deliberately trigger a StackOverflowException without using recursion?

查看:142
本文介绍了如何故意引发不使用递归一个StackOverflowException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我,每一个方法都有一个堆栈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.

这篇关于如何故意引发不使用递归一个StackOverflowException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆