Eclipse-C++-Debugging:查看数组的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1824685/
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
Eclipse-C++-Debugging: see content of an Array
提问by brandstaetter
Is it possible to see the content of a dynamically allocated array, as in:
是否可以看到动态分配的数组的内容,如:
int *array = new int[dimension];
I only see the value of the pointer.
我只看到指针的值。
edit: just found the option "display as an array", but I always have to manually enter the size of the array. Is it possible to get that automagically?
编辑:刚刚找到“显示为数组”选项,但我总是必须手动输入数组的大小。有可能自动获得吗?
回答by Alexander Taylor
In Eclipse, in order to see the content of a dynamically allocated array (for anyone else who stumbles over this question),
在 Eclipse 中,为了查看动态分配的数组的内容(对于遇到此问题的任何其他人),
- Make sure you are in the debugging perspective;
- Look for the "Variables" window. if you don't see it, click "Window" > "Show view" > "Variables";
- Right-click on the array variable;
- Click "display as array...";
- Eclipse does not know how big your array is. So type 0 for the start index and choose the number of elements dynamically allocated for the length. Of course, you can use these values to display any part of the array of your liking.
- 确保您处于调试角度;
- 寻找“变量”窗口。如果没有看到,请单击“窗口”>“显示视图”>“变量”;
- 右键单击数组变量;
- 点击“显示为数组...”;
- Eclipse 不知道您的阵列有多大。因此,为起始索引键入 0 并选择为长度动态分配的元素数。当然,您可以使用这些值来显示您喜欢的数组的任何部分。
And, dealing with a pointer, take note of clicking 'Display as Array' when hovering on the pointer itself (arrow icon), and not on the value it is referenced at first (say in the position of (x)= counts
in the picture).
Otherwise you get an error of the type
并且,处理指针时,请注意在将鼠标悬停在指针本身(箭头图标)上时单击“显示为数组”,而不是最初引用的值(例如在(x)= counts
图片中的位置)。否则你会得到一个类型的错误
Failed to execute MI command:
-data-evaluate-expression [specifics]
Error message from debugger back end:
Cannot access memory at address 0x[address of older*counts
]
无法执行 MI 命令:
-data-evaluate-expression [specifics]
来自调试器后端的错误消息:
无法访问地址 0x[address of old*counts
]处的内存
showing up in the dialogue window just below the list (starting with "Name:" in the screenshot above).
显示在列表下方的对话窗口中(以上面屏幕截图中的“名称:”开头)。
回答by cleong
If you want to avoid having to repeatedly do "Display As Array", open the "Expressions" tab and add the expression (*array@dimension)
. Not sure why the parentheses are necessary. Without them you'd get an error.
如果您想避免重复执行“显示为数组”,请打开“表达式”选项卡并添加表达式(*array@dimension)
。不确定为什么需要括号。没有它们,你会得到一个错误。
回答by Will Rouesnel
In the "Expressions" tab, if you do what cleong noted and type (*array@dimension)
then you can dynamically set the size of the array to display as well. This even works when you need another expression to get it.
在“表达式”选项卡中,如果您按照 cleong 所记下并键入的内容进行操作,(*array@dimension)
那么您也可以动态设置要显示的数组的大小。当您需要另一个表达式来获取它时,这甚至有效。
So say you have a variable x
that contains your array size, you type (*array@x)
and it'll use the content of x as a dimension.
因此,假设您有一个x
包含数组大小的变量,您输入后(*array@x)
它将使用 x 的内容作为维度。
"x" can also be things like struct contents or pointer dereferences and the like - i.e.
"x" 也可以是结构内容或指针取消引用之类的东西 - 即
(*array@SomePtrToStruct->x)
works just fine.
工作得很好。
回答by John Zwinck
just found the option "display as an array", but I always have to manually enter the size of the array. Is it possible to get that automagically?
刚刚找到“显示为数组”选项,但我总是必须手动输入数组的大小。有可能自动获得吗?
That's good. I'd stick with it. Getting the array automatically is not possible in the general case in C or C++, though surely in some trivial cases it could be done (but probably is not, yet--features need to be implemented before they exist, to paraphrase Raymond Chen).
那挺好的。我会坚持下去。在 C 或 C++ 中的一般情况下,自动获取数组是不可能的,尽管在一些微不足道的情况下肯定可以做到(但可能还不是 - 功能需要在它们存在之前实现,用 Raymond Chen 来解释)。