How to display a dynamically allocated array in th

2019-01-03 11:52发布

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?

9条回答
甜甜的少女心
2楼-- · 2019-01-03 12:22

For,

int **a; //row x col

add this to watch

(int(**)[col])a,row
查看更多
趁早两清
3楼-- · 2019-01-03 12:27

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.

查看更多
贼婆χ
4楼-- · 2019-01-03 12:27

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)
查看更多
beautiful°
5楼-- · 2019-01-03 12:28

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;
查看更多
该账号已被封号
6楼-- · 2019-01-03 12:29

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.

查看更多
趁早两清
7楼-- · 2019-01-03 12:34

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

查看更多
登录 后发表回答