Eclipse-C++-Debugging: see content of an Array - c++

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

Related

How can I send complex data to my visualizer in TotalView?

I routinely have to debug legacy Fortran code that is utilizing large arrays of complex data, and the best option available is TotalView. I have created my own visualizer to view data (as per TotalView's instructions here) that works well. It is more flexible than the default one and has the ability to ingest and display complex data, but TotalView will not send complex arrays through its visualization pipe.
Is there any way to make TotalView be able to display complex data without having to recompile the code with additional debugging arrays just to take the absolute value?
E.g. for code like the following short example, I could make another array in Fortran, but I'd really like to just stop and examine the variable my_arr:
program main
implicit none
integer N, M, i, j
parameter (N=100, M=30)
complex my_arr(N, M)
real pi
pi = ACOS(-1.0)
do j = 1, M
do i = 1, N
my_arr(i,j) = cmplx(i*cos(j/pi), i*sin(j/pi))
end do
end do
return
end
For small arrays I can get away with something like this as an expression:
my_arr%Real_Part**2 + my_arr%Imaginary_Part**2
but that won't work for anything very large, TotalView complains about the memory.
I'm using TotalView 8.13.
You can do this if your array is contiguous in memory and you can adjust your visualizer to input the complex data as a real array with an extra dimension containing the real and imaginary parts.
In your example above, if you 'dive' into the variable my_arr, it will show up as type
COMPLEX(4)(100,30)
This is actually the same as the TotalView built-in $complex_8. You can recast the type and dimensions by simply retyping the following into the "Type:" field:
$real_4(2,100,30)
Then the real and imaginary parts will reside in the first (fastest-iterating) dimension and TotalView will allow you to pass the 3D float array to the visualizer. Note: by default TotalView restricts itself to visualizing 2D arrays, so you'll need to change that to 3D (or however many your visualizer can handle) under "Preferences->Launch Strings" in the "Enable Visualizer Launch" box under "Maximum array rank."
Allocatable arrays:
Dynamically sized arrays can be handled in the same way, but require a couple extra steps.
Usually the address of the reference to the array is not the address of the actual array in memory, so you will have to manually adjust the address of the dive window.
In the dive window on the right side there is an option button just above the scroll bar to indicate what columns are shown in the window - turn on "Address" and write down the hex address of the first element in the array. After you recast by changing the type string, type that hex address into the "Address" field at the top, then your data will show up correctly.
The type string will contain something along the lines of COMPLEX(4),allocatable::(:,:), whereas the "Actual Type" string will show you the dimensions. When you do the recast, make sure to remove the ,allocatable:: and change the (:,:) to the actual dimensions (e.g. (100,30)).

Apply a function to a range of cells in a spreadsheet

The answers in topics with similar titles haven't given me much of a resolution to my particular problem, but possibly I am not asking the right question. It might help knowing I'm an absolute noob when it comes to spreadsheets, so finding my way around is next to nil.
Currently I can set a basic function in the first cell A1 =ROW()
Simple right? Well now here comes the complication. If I click on the bottom right of the cell and start dragging I can then apply that very same function to a whole range of cells. Let's say I apply it from A1:A10. Every cell within this group now has the same function.
Hooray! We did it, right? I applied a function to a range of cells each with their own output. But wait, if I then go back to the original cell and change its formula none of the other cells change with it. GRRRRR!!!!
There are a couple of fixes I've come up with but don't necessarily know how to implement. The first is to have every cell link back to the original cell and reference its function. This would be useful if I wanted to randomly scatter dependent cells about the document. The other would be much more useful in an orderly group where you know the exact dimensions by specifying in the original cell the size of the array you want to apply the function to.
With that said, let me hear your thoughts.
The closest I've come to an answer is to use FORMULA() which returns the formula used by a cell as text. Unfortunately all answers on evaluating the text resort to scripting. How strange! I thought something like this would be common. Might as well get to scripting.
Hold on, I may have spoke too soon. An array can be made with =MUNIT(), but it's only square. Drats!
Ok... I'm hoping the zebra stripes will eventually become its own answer unless someone else beats me to it. So a simple array can be made with ={1,2;3,4} where commas separate values by column and semicolons for values by row except to generate it you have to press Control+Shift+Enter (because reasons?). I'm thinking now that I'll need to have functions that can generate lists of values based on a single function for each row, and pray that it'll work. So, back to looking. (Wow this is taking forever)
The way I was hypothesizing can't even generate a 1x1, e.g., ={ROW()} returns Err:512 which is a formula overflow.
Alright, in summary so far I've narrowed down the two options,
1) link every cell to the original formula
2) populate an array with a single formula
each with their own incomplete answer,
a) use FORMULA() to return the formula of a cell as text
b) create a hypothetical array like so ={LIST_OF_VALUES()}
These both require a strange form of the nonexistent EVALUATE() function to 'function' correctly. Isn't that fun?
Google Sheets handles case b by allowing ={ROW()}Control+Shift+Enter to generate =ArrayFormula({ROW()}). Working with the general case of any sized array being filled with a single function doesn't exist in the world of spreadsheets it seems. That's very saddening because I can't think of a much better tool for what I want to do. Copy paste it is until I need to use macros.
Depending on your specific use case, creating a user-defined function may help:
use the Basic IDE to create your function;
apply it to any cells on any sheet;
modifying the Basic code will affect all cells where the function is used.
I've elaborated the steps in an answer on superuser.
Sure, you could write some complex code to update functions, but wouldn't the easy way be just to drag it to the same range of cells the same way you did before? It should properly overwrite the existing code in there, and if it doesn't, you can just as easily delete the outdated code and drag the new code in.
Probably the best approach is to simply drag the amended formula over the range of cells (as advised by OldBunny2800). This is less error prone and easier to maintain than a custom macro.
Another option would be to use an array function. Then you only have to edit the function once, and the same edit will be automatically applied to the whole range of cells in that array function.

Debugger's watch window shows the variables values as zero but they are not

I have structure statically allocated. It has couple of arrays. When I see the values of these arrays they are displayed but all values are zero (0). When I werite this array to file, the values are correct however if I watch the values at the same time in debugger the values show up as zero. I used TRACE to print the values on output window and that is also correct.
So the program is doing all computation correctly but debugger shows the variables' values all zero. I am using VS2010 and C++. Is there a way to fix it?
p.s I have tried other solutions listed around like typing array_name,number in debugger but that doesn't work for me either.

wxChoice::SetString() Index Error

I've finally gotten around to messing with the wxChoice control in my application, and for some reason whenever I attempt to modify the text of a column, I get the error:
....\src\msw\choice.cpp(348): assert "IsValid(n)" failed in
wxChoice::SetString(): invalid item index in wxChoice::SetString
The error itself makes it quite obvious that the item index isn't valid, but based off of my code and the (not very descriptive) documentation, I should only need to set the number of columns, then I'd be allowed to edit the text at a given index.
Here's a snippet from my project:
int phones = Phones->size();
choiceDevice->SetColumns(phones); // Value greater than 0
choiceDevice->SetString(phones - 1, pInstance->ProfileName); // Should assign the last index the value of the ProfileName.
What if Phones is empty, and thus phones == 0?
wxChoice::SetColumns
Remarks:
This is implemented for GTK and Motif only and doesn’t do anything under other platforms.
It looks like SetColumns is used for multi-column choice control, I have never seen it. Anyway, it doesn't add new items to the control. In Windows it does nothing in any case.
As Alex had said, I shouldn't be using wxChoice::SetColumns on Windows since it has no affect. I found that I have to use wxChoice::Append instead of setting the number of columns, then setting the individual string.

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. 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?
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:
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 =
m4x4,16
This displays data in a list:
Another way is with a Memory window (Debug/Windows/Memory). 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.
In a watch window, add a comma after the name of the array, and the amount of items you want to be displayed.
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;
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.
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.
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)
For MFC arrays (CArray, CStringArray, ...)
following the next link in its Tip #4
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
pArray.m_pData,5
to see the first 5 elements .
If pArray is a two dimensional CArray you can look at any of the elements of the second dimension using the next syntax:
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]
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).