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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/75180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:39:47  来源:igfitidea点击:

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

c++cvisual-studiodebugging

提问by Adam Rosenfield

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?

如果您有一个静态分配的数组,Visual Studio 调试器可以轻松显示所有数组元素。但是,如果您有一个动态分配并由指针指向的数组,则当您单击 + 将其展开时,它只会显示该数组的第一个元素。有没有一种简单的方法可以告诉调试器,将此数据显示为类型为 Foo 且大小为 X 的数组?

回答by shoosh

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.

将向您显示内容,就好像它是一个数组一样。

回答by Riaz Rizvi

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). Add watch =

一种方法是使用 Watch 窗口 (Debug/Windows/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 =

另一种方法是使用内存窗口(调试/Windows/内存)。指定内存起始地址 =

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.

右键单击“内存”窗口以确定二进制数据的可视化方式。选择仅限于整数、浮点数和一些文本编码。

回答by Drealmer

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

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

回答by Gengpu Liu

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;

回答by vicky

For,

为了,

int **a; //row x col

add this to watch

添加此观看

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

回答by Taylor Price

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. Many different string variants, variants to print individual items in the array, etc.

有关所有可能的不同转换,请参阅 MSDN 页面上的表格,因为有很多。许多不同的字符串变体、用于打印数组中单个项目的变体等。

回答by Legolas

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)

回答by dabinsi

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

回答by wog

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. (I'm using VS2008, this shows only 25 elements maximum).

将显示 ne "next" 25 个元素。(我使用的是 VS2008,这最多只显示 25 个元素)。