256

Is it possible to view an array in the Visual Studio debugger? QuickWatch only shows the first element of the array.

John Weldon
  • 37,037
  • 10
  • 91
  • 126
user20493
  • 5,244
  • 7
  • 31
  • 31
  • 3
    http://support.microsoft.com/kb/198953 – becko Aug 22 '12 at 19:35
  • If it is a CArray, writing array.m_pData[pos] or array->m_pData[pos] (depending on the case) on the Watch Window lets you see the contents of array at the pos position! – sergiol Jul 15 '14 at 01:13

5 Answers5

608

You can try this nice little trick for C++. Take the expression which gives you the array and then append a comma and the number of elements you want to see. Expanding that value will show you elements 0-(N-1) where N is the number you add after the comma.

For example if pArray is the array, type pArray,10 in the watch window.

Jaanus
  • 15,013
  • 43
  • 137
  • 193
JaredPar
  • 673,544
  • 139
  • 1,186
  • 1,421
  • 15
    Incredible find. It does however only work with immediate values, no arithmetic or dynamic member access is allowed. – Sebastian Graf Oct 29 '12 at 15:40
  • 33
    Note that you can also use a cast in the debug view. If `pArray` is of type `void*` you can type `(char*) pArray, 10` which will display the content of the array interpreted as char. – VoidStar Aug 15 '13 at 09:26
  • Cannot do this in Visual Studio 2008 Express. If I add an expression "a+1,2" in the Watch Window, the foloowing error will occur: "error: + cannot be performed on 'pArray' and '1'". What version of VS are you using? – An Cong Tran Jun 14 '14 at 17:03
  • Anyone know if something similar is available in Eclipse? – dtmland Feb 25 '15 at 23:40
  • 4
    I think you're probably using GDB as the debugger in Eclipse, in which case the equivalent syntax, `*pArray@10` is what you're looking for. I haven't tried it in Eclipse, but it works in command-line GDB. – Tom Jun 13 '16 at 14:08
  • what about c# ? – Viku Feb 23 '17 at 07:27
  • I don't think this is possible in c#, unfortunately. VS uses a far more robust set of format specifiers for c++ than it does for c# for some reason. https://docs.microsoft.com/en-us/visualstudio/debugger/format-specifiers-in-csharp?view=vs-2019 – b1nary.atr0phy Sep 12 '19 at 05:15
91

If you have a large array and only want to see a subsection of the array you can type this into the watch window;

ptr+100,10

to show a list of the 10 elements starting at ptr[100]. Beware that the displayed array subscripts will start at [0], so you will have to remember that ptr[0] is really ptr[100] and ptr[1] is ptr[101] etc.

Ken
  • 3,635
  • 3
  • 21
  • 37
25

I use the ArrayDebugView add-in for Visual Studio (http://arraydebugview.sourceforge.net/).

It seems to be a long dead project (but one I'm looking at continuing myself) but the add-in still works beautifully for me in VS2010 for both C++ and C#.

It has a few quirks (tab order, modal dialog, no close button) but the ability to plot the contents of an array in a graph more than make up for it.

Edit July 2014: I have finally built a new Visual Studio extension to replace ArrayebugView's functionality. It is available on the VIsual Studio Gallery, search for ArrayPlotter or go to http://visualstudiogallery.msdn.microsoft.com/2fde2c3c-5b83-4d2a-a71e-5fdd83ce6b96?SRC=Home

Rodney Thomson
  • 593
  • 5
  • 11
  • Looks great but doesnt support VS2013?? VSIXInstaller.NoApplicableSKUsException: This extension is not installable on any currently installed products. Supported Products : Microsoft.VisualStudio.Pro Version : [10.0] Version : [11.0] Version : [12.0] Version : [14.0] – Gregory Sep 28 '14 at 07:44
  • 2
    It should do! I developed it in VS2013! What version of VS2013 (service pack / edition) are you running? If you search for ArrayPlotter in the Extension Manager (in the Online section) do you see it available within there? – Rodney Thomson Oct 16 '14 at 04:11
  • Beautiful tool, works fine for me in VS2013. – Reinier Torenbeek Apr 21 '15 at 19:05
16

Are you trying to view an array with memory allocated dynamically? If not, you can view an array for C++ and C# by putting it in the watch window in the debugger, with its contents visible when you expand the array on the little (+) in the watch window by a left mouse-click.

If it's a pointer to a dynamically allocated array, to view N contents of the pointer, type "pointer, N" in the watch window of the debugger. Note, N must be an integer or the debugger will give you an error saying it can't access the contents. Then, left click on the little (+) icon that appears to view the contents.

stanigator
  • 10,102
  • 32
  • 88
  • 126
0

Hover your mouse cursor over the name of the array, then hover over the little (+) icon that appears.

RichieHindle
  • 244,085
  • 44
  • 340
  • 385