Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to see how a C# array is laid in memory?

I'd like to see how the C# array is laid in computer memory.

What I'd like to see is mainly two columns one with addresses second with array elements. Is it possible?

I'd like to start with 1D array but then I'd like to observe how multidimensional arrays are laid.

Question

How can I see it via VisualStudio ?

like image 904
user1146081 Avatar asked Mar 02 '15 16:03

user1146081


1 Answers

You can use the Visual Studio debugger to see the array layout. A simple example:

    static void Main(string[] args) {
        int[] arr = { 1, 2, 3 };
        Console.ReadLine();  // Breakpoint here
    }

Use Project + Properties, Build tab, Platform target = x86. Set a breakpoint on the indicated line, press F5, when it hits use Debug + Windows + Memory + Memory 1. Type arr in the Address box. Right-click the window and select "4 byte Integer". Looks like this:

enter image description here

The first word is the "type handle", its value is random, just ignore it. You can no doubt guess the rest, you see the array Length and the array elements.

like image 147
Hans Passant Avatar answered Sep 19 '22 01:09

Hans Passant