Memory layout of a .NET array

前端 未结 4 1232
醉梦人生
醉梦人生 2020-12-02 18:43

What is the memory layout of a .NET array?

Take for instance this array:

Int32[] x = new Int32[10];

I understand that the bulk of t

4条回答
  •  既然无缘
    2020-12-02 18:47

    Great question! I wanted to see it for myself, and it seemed a good opportunity to try out CorDbg.exe...

    It seems that for simple integer arrays, the format is:

    ssssllll000011112222....nnnn0000
    

    where s is the sync block, l the length of the array, and then the individual elements. It seems that there is a finally 0 at the end, I'm not sure why that is.

    For multidimensional arrays:

    ssssttttl1l1l2l2????????
        000011112222....nnnn000011112222....nnnn....000011112222....nnnn0000
    

    where s is the sync block, t the total number of elements, l1 the length of the first dimension, l2 the length of the second dimension, then two zeroes?, followed by all the elements sequentially, and finally a zero again.

    Object arrays are treated as the integer array, the contents are references this time. Jagged arrays are object arrays where the references point to other arrays.

提交回复
热议问题