c# - 初始化数组时出现 StackOverflow 异常
问题描述
我调用该方法并获取 StackOverflowException。它不是递归调用,它只包含数组初始化。我需要一个 BigIntegers 数组,该代码适用于 int 数组,即使大小更大。我展示了简化的示例,在实际代码中我不能使用循环来填充数组,因为我无法生成我需要的数字,所以我必须对它们全部进行硬编码。
设置:x64 模式,.Net Core
从错误细节我们可以看到:
1) 堆栈跟踪为空
2) 错误可能源自 System.Collections.ListDictionaryInternal
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Before"); // <--- This is displayed
var a = GetBigIntegers(); // <--- Method is called
Console.WriteLine("After"); // <--- We will never get there
}
static BigInteger[] GetBigIntegers()
{
// <--- Crash here
return new BigInteger[]
{
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
// Many more lines (850-900) and they are 2-3 times longer than here
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
};
}
}
我检查了 IL 代码,它看起来是正确的,它需要接近 400 000 行。
.method private hidebysig static
valuetype [System.Runtime.Numerics]System.Numerics.BigInteger[] GetBigIntegers () cil managed
{
// Method begins at RVA 0x207c
// Code size 1130123 (0x113e8b)
.maxstack 4
.locals init (
[0] valuetype [System.Runtime.Numerics]System.Numerics.BigInteger[]
)
// (no C# code)
IL_0000: nop
IL_0001: ldc.i4 66500
IL_0006: newarr [System.Runtime.Numerics]System.Numerics.BigInteger
IL_000b: dup
IL_000c: ldc.i4.0
// return new BigInteger[66500]IL_000d: ldc.i4.1
IL_000e: call valuetype [System.Runtime.Numerics]System.Numerics.BigInteger [System.Runtime.Numerics]System.Numerics.BigInteger::op_Implicit(int32)
// (no C# code)
IL_0013: stelem [System.Runtime.Numerics]System.Numerics.BigInteger
IL_0018: dup
IL_0019: ldc.i4.1
IL_001a: ldc.i4.1
IL_001b: call valuetype [System.Runtime.Numerics]System.Numerics.BigInteger [System.Runtime.Numerics]System.Numerics.BigInteger::op_Implicit(int32)
IL_0020: stelem [System.Runtime.Numerics]System.Numerics.BigInteger
.....
IL_113e75: dup
IL_113e76: ldc.i4 66499
IL_113e7b: ldc.i4.1
IL_113e7c: call valuetype [System.Runtime.Numerics]System.Numerics.BigInteger [System.Runtime.Numerics]System.Numerics.BigInteger::op_Implicit(int32)
IL_113e81: stelem [System.Runtime.Numerics]System.Numerics.BigInteger
IL_113e86: stloc.0
IL_113e87: br.s IL_113e89
IL_113e89: ldloc.0
IL_113e8a: ret
} // end of method Program::GetBigIntegers
我预计该数组将被初始化并返回,但实际上我得到了 StackOverflow 错误。
我知道我可以使用不同的方法来做同样的事情,但我想知道为什么它不能这样工作。希望每个阅读这个问题的人也很有趣。
解决方案
推荐阅读
- arrays - 从 Node.JS AWS Lambda 中的回调数据数组返回单个值
- c++ - 无法在 Visual Studio 2017 中同时弹出 2 个子窗口
- python - 提取嵌入在网页代码中的 JSON
- python - 图像旋转后如何获得新坐标?
- spring - 为什么 spring jpa 存储库总是在 @GetMapping 方法中急切加载?
- winforms - 如何在自升式 Powershell 脚本中隐藏控制台而不是 GUI
- r - 连接两个字符串,删除重复项
- python - 如何将交互式opencv窗口集成到pyqt5小部件中
- llvm - llvm GEP 第一种类型的目的是什么
- spring - Spring Data JPA - 让实体跟踪有多少实体引用他