简体   繁体   English

如何在 Visual Studio 调试器中显示动态分配的数组?

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

If you have a statically allocated array, the Visual Studio debugger can easily display all of the array elements.如果您有一个静态分配的数组,Visual Studio 调试器可以轻松显示所有数组元素。 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?有没有一种简单的方法可以告诉调试器,将此数据显示为类型为 Foo 且大小为 X 的数组?

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.将向您显示内容,就好像它是一个数组一样。

There are two methods to view data in an array m4x4:有两种方法可以查看数组 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).一种方法是使用 Watch 窗口 (Debug/Windows/Watch)。 Add watch =添加手表 =

m4x4,16

This displays data in a list:这将在列表中显示数据:

在此处输入图片说明

Another way is with a Memory window (Debug/Windows/Memory).另一种方法是使用内存窗口(调试/Windows/内存)。 Specify a memory start address =指定内存起始地址 =

m4x4

This displays data in a table, which is better for two and three dimensional matrices:这在表格中显示数据,这对于二维和三维矩阵更好:

在此处输入图片说明

Right-click on the Memory window to determine how the binary data is visualized.右键单击“内存”窗口以确定二进制数据的可视化方式。 Choices are limited to integers, floats and some text encodings.选择仅限于整数、浮点数和一些文本编码。

在监视窗口中,在数组名称和要显示的项目数量后添加逗号。

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:那么您可以在 Visual Studio 调试监视中编写以下内容:

(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;

For,为了,

int **a; //row x col

add this to watch添加此观看

(int(**)[col])a,row

Yet another way to do this is specified here in MSDN .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:您可以使用以下命令在监视窗口中将其打印为 unicode 字符串:

a,su

See the tables on the MSDN page for all of the different conversions possible since there are quite a few.有关所有可能的不同转换,请参阅 MSDN 页面上的表格,因为有很多。 Many different string variants, variants to print individual items in the array, etc.许多不同的字符串变体、用于打印数组中单个项目的变体等。

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您可以在文档中的此 gem 的监视窗口中找到可以对变量执行的许多操作的列表: https : //msdn.microsoft.com/en-us/library/75w45ekt.aspx

For a variable a, there are the things already mentioned in other answers like对于变量a,其他答案中已经提到了一些事情,例如

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)

For MFC arrays (CArray, CStringArray, ...) following the next link in its Tip #4对于 MFC 数组(CArray、CStringArray、...),请点击其提示 #4 中的下一个链接

http://www.codeproject.com/Articles/469416/10-More-Visual-Studio-Debugging-Tips-for-Native-De 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例如对于“CArray pArray”,在观察窗口中添加

     pArray.m_pData,5 

to see the first 5 elements .看前5个元素。

If pArray is a two dimensional CArray you can look at any of the elements of the second dimension using the next syntax:如果 pArray 是二维 CArray,您可以使用以下语法查看第二维的任何元素:

     pArray.m_pData[x].m_pData,y

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,它应该看起来像这样:

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

You can also use an offset您还可以使用偏移量

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

will show ne "next" 25 elements.将显示 ne "next" 25 个元素。 (I'm using VS2008, this shows only 25 elements maximum). (我使用的是 VS2008,这最多只显示 25 个元素)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM