How to display a dynamically allocated array in the Visual Studio debugger?


Question

If you have a statically allocated array, the Visual Studio debugger can easily display all of the array elements. However, if you have an array allocated dynamically and pointed to by a pointer, it will only display the first element of the array when you click the + to expand it. Is there an easy way to tell the debugger, show me this data as an array of type Foo and size X?

1
142
12/5/2011 12:42:44 PM

Accepted Answer

Yes, simple. say you have

char *a = new char[10];

writing in the debugger:

a,10

would show you the content as if it were an array.

187
4/18/2012 8:42:26 AM

There are two methods to view data in an array m4x4:

float m4x4[16]={
    1.f,0.f,0.f,0.f,
    0.f,2.f,0.f,0.f,
    0.f,0.f,3.f,0.f,
    0.f,0.f,0.f,4.f
};

One way is with a Watch window (Debug/Windows/Watch). Add watch =

m4x4,16

This displays data in a list:

enter image description here

Another way is with a Memory window (Debug/Windows/Memory). Specify a memory start address =

m4x4

This displays data in a table, which is better for two and three dimensional matrices:

enter image description here

Right-click on the Memory window to determine how the binary data is visualized. Choices are limited to integers, floats and some text encodings.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon