vex*_*exe 3 c# memory heap stack
在C#中,当我声明一个这样的数组时:
int []a = {1, 2, 3, 4, 5}
Run Code Online (Sandbox Code Playgroud)
这是在堆栈中声明的吗?
据我所知,我应该这样做:
int []a = new int[5]{1, 2, 3, 4, 5};
Run Code Online (Sandbox Code Playgroud)
考虑到数组是引用类型.
实际上有一种方法可以在堆栈中存储数组:
(来自http://www.c-sharpcorner.com/uploadfile/GemingLeader/creating-a-stack-based-array/)
int* pArr = stackalloc int[length];
Run Code Online (Sandbox Code Playgroud)
但由于使用了指针,这需要一个不安全的代码区域.