Array of ValueType in C# goes to Heap or Stack?

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?


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:

public class X
{
    public int Y;
}

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.


That does not really matters, due you do not have direct access neither to cpu stack nor cpu heap, so you can not control it. and if you can not control it, you do not need to worry about it.

You can do little about it in case of StackOverflowException or MemoryAllocationError to restore critical data in that case

链接地址: http://www.djcxy.com/p/78882.html

上一篇: 为什么有可能在没有新关键字的情况下实例化一个结构体?

下一篇: C#中的ValueType数组转到堆或堆栈?