English 中文(简体)
Eclipse-C++-Debugging: see content of an Array
原标题:

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?

最佳回答

In Eclipse, in order to see the content of a dynamically allocated array (for anyone else who stumbles over this question),

  1. Make sure you are in the debugging perspective;
  2. Look for the "Variables" window. if you don t see it, click "Window" > "Show view" > "Variables";
  3. Right-click on the array variable;
  4. Click "display as array...";
  5. 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.

choose display as array in eclipse

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

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]

showing up in the dialogue window just below the list (starting with "Name:" in the screenshot above).

问题回答

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.

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.

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" can also be things like struct contents or pointer dereferences and the like - i.e.

(*array@SomePtrToStruct->x) 

works just fine.

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).





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签