Remember my choice for display as array in Eclipse C/C++ - eclipse-cdt

In debug mode of Eclipse for C/C++, you can right-click on any watched expression that is a pointer and select "display as array". Then you're prompted for the array bounds and for the rest of this debug run, the watched expression is being displayed as an array according to these bounds.
When I terminate the process and start debugging again, it remembers my watched expressions, but pointers that were previously displayed as arrays will now be mere pointers again and hence, I have to re-cast all pointers in each debug run. In a recent project, this has become very tiresome.
Is there a way to make Eclipse remember the "display as array" choices for watched expressions?

You have to be able to encode the fact that you want to look at the pointer as an array in the expression string itself.
Say you have an array as int* and you want to look at (most at) its first 4 elements.
In the Expressions tab, use one of the two following syntaxes supported by GDB:
(*arr # 4)
((int[4])*arr)
The surrounding (...) parens above are important.
You can do this in the Expressions tabs (watches), but not in the Variables tab.

Related

Debugger problem regarding to how to see all the value in the dynamic array in c++

I need to understand the other people's code by debugging through all the code, in the meantime, I can't change or add any code. The one major problem comes up. That is When I want to see the all the value in a dynamic array by adding it to Watch, it would not give me what I expect to. I am only able to see first value of the them. Such as:
In addition, when I add it to Watch, it would show no operator "[]" matches these operands.
How can i address this problem?
you can follow this link, it shows you how to use feature called the “size specifier”to display the array elements:
https://blogs.msdn.microsoft.com/habibh/2009/06/05/size-specifier-how-to-display-a-cc-pointer-as-an-array-in-the-visual-studio-debugger/

Problem with input whenever debugging C++ (Xcode 10.1)

I am newbie to Xcode. I try to create a first problem with C++ (image below) and set some breakpoints to test debugging. I get a problem with input in console window. Particularly, whenever I get input line, I type number and there is nothing i can see in console until i change between target output and all output (i.e. if i am on "target output" and type anything, i have to change into "all output" to see the number i typed, vice versa). And there is one more problem: I can delete the number i typed, i.e. if i type 3, it only allow me to add postfix number such as 31 or anything like it. Cannot change the number.
Does anyone know this problem? Please help me. Thank a lot.
This is my code
in the old way you were allocating an empty array you should give its size which is 'n' here
int *a=new int(n);
your old code had some undefined behavior due to the out of bound access you were trying to do accessing without allocating
at the end of your code you should deallocate the dynamic allocated memory like this :
delete[] a;

Debugging with gdb - (gdb) "x/s pointer" output

I am trying to debug using gdb. I got that if you want output in string you have to use "x/s Ptr". It works fine some time. But many times I am getting either Null value i.e. " " or some random numeric values. My file has 10000 lines of codes. :-p Please find some gdb output. For e.g.
krb5_get_credentials_for_user (context=0x59c00eb0, options=4, ccache=0x5a001d40, in_creds=0x5ab022a8, subject_cert=0x0,
out_creds=0x5ab02378) at test_abc.c:696
(gdb) x/s 0x59c00eb0
0x59c00eb0: "$\247\016\227"
(gdb) x/s 0x5ab022a8
0x5ab022a8: ""
Could someone please tell me how I can solve this prob? Thanks in advance!
But many times I am getting either Null value i.e. " " or some random numeric values.
There is nothing wrong with what you show. It's just that the memory location you are examining isn't pointing to a string (0x59c00eb0) or is pointing to an empty string (0x5ab022a8).
You didn't present any evidence that these locations should be pointing to a string, and in fact, as (now deleted) comment showed context points to struct _krb5_context, which contains magic number as the first member. Therefore, you should use x/w to examine it.
... fine some times. But many times I am getting either Null value i.e. " "
or some random numeric...
Been there, done that. Allow me to encourage you to be creative.
I sometimes create a function (call it foo? bar? show? dump?), that is not used by the program being debugged. The function is often c-style (because gdb seems to understand that better, and simpler to invoke), global scope, simple. The temporary install of this function close (in the same file?) to what you want to improve the visibility of sometimes helps.
I can then invoke this function using the gdb p command, such as
gdb> p foo
it is possible to pass parameters to foo, but if I'm touching the code to debug something, I usually make foo more capable ... when no parameters it does one thing. Or perhaps use an int parameter (bar(7)) that switches to show more or differently.
Experiment.
This is not typical, and I suspect better knowledge of gdb might be worth the effort, if I could remember it to the next time I need it. Sometimes gdb just doesn't understand, and I can't figure out why. Other times, I get away with adding a pointer and trying to print that:
gdb> p *foobar

An easy way to watch a vector element in debugger

I'm using Visual Studio 2013 and in its long history it was never able to show a vector element in debugger, complaining with no operator "[]" matches these operands message. I know that there is a workaround requiring typing v.operator[](n), but this is not acceptable for me. I want to hover the cursor above v[n] and see its value or at the most select or cut and paste v[n] to see the value. Is it possible with other Windows C++ IDEs?
I know that all elements of vector are shown in Autos and Locals windows, but my vectors are too long for this to be practical.
Just prefix each [] with _Myfirst in the Watch field:
YourVector._Myfirst[n]
Trick here:
Say you have an std::vector<int> v; and you want to see in the watch v[23] or maybe v[23]..v[23+n] do this:
Add the variable to the watch windows.
Add ,! after the name of the variable (ex: v,!) this indicate VS that you want to turn off debugger visualization.
Expand vector members until you see _Myfirst, _Mylast and _Myend. Add _Myfirst to the watch. This is the pointer to the beginning of the vector memory.
Erase v,! from the watch if you want.
To _Myfirst element added to the watch add at the end + offset, count where offset is the vector index you want to see first in the watch and count is the numbers of element of the vector you want to see. Would be something like this: (*((std::_Vector_val<std::_Simple_types<int> >*)(&(*((std::_Vector_alloc<0,std::_Vec_base_types<int,std::allocator<int> > >*)(&(v)))))))._Myfirst + 23, 100. This let you see 100 elements of the vector starting in position 23 (yes I known it's large the _Myfirst element). You could specify offset and count using variables (ex: to match an expression in the code like v[n] use as offset n and count whatever you want, constant or variable.
Some info about Debugging Tips and Tricks, Going Native Episode 28 from minute 17 have some goodies, the evaluation expression could be in comments. Example you have some code.
v[n] = ... + pre_calculate(v[n]) + ...
// You could put a comment like this:
// (*((std::_Vector_val<std::_Simple_types<int> >*)(&(*((std::_Vector_alloc<0,std::_Vec_base_types<int,std::allocator<int> > >*)(&(v)))))))._Myfirst + n, 100
// And when you hover the mouse over the selected expression, you see the evaluation. Much better I think.
In MSVC's implementation of the standard library, _M_start, _M_finish, and _M_end_of_storage are public members in _Vector_base that can be used.
vector._M_start[n]
This method will be valid for any C++ IDE youre using; First you have to know the vector member name wich stores the first element.
(In borland 6 c++) you can do this by inspecting the vector once you have already added to the watches window. In Visual it is called different.
Then you have to add the following syntax in watches:
nameVector.memberWichPointsToTheFirstElement[startIndex],numElementsDesiredToDisplay.
And your done. But when the vector is inside an instance, it will not show anything saying: side effects are not allowed.

Display a pointer value with lldb debugger

I'm working in a personal project of open-source technologies developing an application build it in C. I'm using the lldb debugger tool.
My question is simple: How can I display or show the values of an element when I'm debugging.
For example:
#include <iostream.h>
int main(){
char phrase[1024];
int i=0;
for(i=0;i<1024;i++){
printf("%c",phrase[i]);
}
return 0;
}
In the lldb prompt, I can see the values for specific character of the array:
lldb>b 6
lldb>frame variable phrase[0];
When I want to execute:
lldb>frame variable phrase[i]
I got an error: "unable to find any variable expression path that matches 'phrase[i]'"
You need to use
(lldb) expr phrase[i]
or equivalently
(lldb) p phrase[i]
for that
frame variable supports constant indexes (i.e. plain ol’ numbers), but if you need to use a variable or anything BUT a number, you need to use the expression command
As a caveat, the behavior of frame var vs. expression might be different in some cases when doing array-like access. This won’t affect your example (but it would if you were using an std::vector, for instance).