C#中ValueType的数组是存放在堆还是堆栈中?
可能的重复:
(C#) 数组、堆和堆栈以及值类型 < /p>
I我正在尝试研究 C# 中内存分配之间的一些差异
假设我有这样的指令:
int[] array = new int[512];
“数组”是否转到堆?或者它在堆栈上保留512个整数?
Possible Duplicate:
(C#) Arrays, heap and stack and value types
I am trying to study some differences between memory allocation in c#
Let's suppose that I have this instruction:
int[] array = new int[512];
Does the "array" go to the Heap? Or does it reserve 512 integers on Stack?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数组本身是在堆上分配的内存。人们会感到困惑,并认为值类型总是在堆栈上,但这并不总是正确的。
例如,在这个类中:
尽管Y是一个int(值类型),但由于它包含在一个类(引用类型)中,因此int Y将与对象本身一起存储,而对象本身很可能位于堆上。
数组类似。 int 作为值类型包含在数组中,但数组本身位于堆上,因此 int 也在堆上。
The array itself is memory allocated on the heap. People get confused and think value types are always on the stack and this is not always correct.
For example, in this class:
Even though Y is an int (value type), since it is contained in a class (reference type), the int Y will be stored with the object itself, which is most likely on the heap.
The array is similar. The ints are contained in the array as value types, but the array itself is on the heap, and thus the ints are too.