162

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?

0

9 Answers 9

223

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.

8
  • 12
    That's a great trick, but if your pointer points to an array of structures, I've found that the individual elements expanded with "a,10" in the watch window aren't themselves expandable. Meaning you can't dig into the 3rd element of the array using this method. Is that something that can be overcome?
    – SirPentor
    Commented May 11, 2012 at 18:20
  • @SirPentor I have the same issue. Have you found a solution?
    – a06e
    Commented Aug 22, 2012 at 19:43
  • @becko--negatory. It's frustrating.
    – SirPentor
    Commented Aug 23, 2012 at 1:51
  • 2
    For beginners: If you select "a" variable, right click and add to watch list (inspect), if you open de debugger view in the list of watched values (I can't find the name of the window right now), you can double click "a" and rename it "a,X" where X is the number of items. You'll see now all the values.
    – Darkgaze
    Commented May 19, 2017 at 16:02
  • 1
    Does anyone know how to use the same feature in Visual Studio Code IDE for C++.
    – kapilgm
    Commented Nov 25, 2019 at 1:35
41

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.

5
  • 1
    THIS IS LIFE-CHANGING. Commented Jan 11, 2022 at 19:42
  • @orionelenzil vote it up if you really feel that way?
    – Rian Rizvi
    Commented Jan 11, 2022 at 21:11
  • 1
    wups, forgot, thank you ! I've been spending a lot of time recently on a site where appreciation is mostly indicated by commenting. Commented Jan 11, 2022 at 21:56
  • Notice that the 1st method only works with statically allocated arrays, the second method works for both statically and dynamically allocated arrays
    – Victor
    Commented Jun 11, 2024 at 15:57
  • 1
    Great thanks for the explanation, most liked answer didn't explain all that
    – Max Cury
    Commented Dec 24, 2024 at 8:55
25

In a watch window, add a comma after the name of the array, and the amount of items you want to be displayed.

12

a revisit:

let's assume you have a below pointer:

double ** a; // assume 5*10

then you can write below in Visual Studio debug watch:

(double(*)[10]) a[0],5

which will cast it into an array like below, and you can view all contents in one go.

double[5][10] a;
7

For,

int **a; //row x col

add this to watch

(int(**)[col])a,row
1
  • Please, tell me, I can't find "watch". Where is it?
    – Egor
    Commented Apr 21, 2019 at 12:48
6

Yet another way to do this is specified here in MSDN.

In short, you can display a character array as several types of string. If you've got an array declared as:

char *a = new char[10];

You could print it as a unicode string in the watch window with the following:

a,su

See the tables on the MSDN page for all of the different conversions possible since there are quite a few. Many different string variants, variants to print individual items in the array, etc.

1
  • 1
    From the MSDN link you gave -- a,[10] allows you to see individual elements so that they themselves are expandable, even if you have a CArray of complex data types.
    – LThode
    Commented May 5, 2015 at 14:10
2

For MFC arrays (CArray, CStringArray, ...) following the next link in its Tip #4

http://www.codeproject.com/Articles/469416/10-More-Visual-Studio-Debugging-Tips-for-Native-De

For example for "CArray pArray", add in the Watch windows

     pArray.m_pData,5 

to see the first 5 elements .

If pArray is a two dimensional CArray you can look at any of the elements of the second dimension using the next syntax:

     pArray.m_pData[x].m_pData,y
2

You can find a list of many things you can do with variables in the watch window in this gem in the docs: https://msdn.microsoft.com/en-us/library/75w45ekt.aspx

For a variable a, there are the things already mentioned in other answers like

a,10 
a,su 

but there's a whole lot of other specifiers for format and size, like:

a,en (shows an enum value by name instead of the number)
a,mb (to show 1 line of 'memory' view right there in the watch window)
0

I haven't found a way to use this with a multidimensional array. But you can at least (if you know the index of your desired entry) add a watch to a specific value. Simply use the index-operator.

For an Array named current, which has an Array named Attribs inside, which has an Array named Attrib inside, it should look like this if you like to have to position 26:

((*((*current).Attribs)).Attrib)[26]

You can also use an offset

((*((*current).Attribs)).Attrib)+25

will show ne "next" 25 elements. (I'm using VS2008, this shows only 25 elements maximum).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.