user1146081
user1146081

Reputation: 195

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 ?

Upvotes: 6

Views: 666

Answers (2)

Hans Passant
Hans Passant

Reputation: 941457

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.

Upvotes: 14

aush
aush

Reputation: 2108

You can use WinDbg with sos or psscor extensions.

Upvotes: 0

Related Questions