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.
Related
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/
I am new to LLDB and I am working with various std::vectors in my code, however when I try to print the values of a vector or to query the size of my vector with something like expr '(int)myVector[0]' or expr '(int)myVector.size()' the debugger prints values that have nothing to do with the values I know there are in the vector.
As I'm learning to debug with command line and LLDB, I'm sure I'm missing something here, can anyone spot my error or give some advise?
EDIT Forgot to say that I'm under OS X Mavericks with the latest command-line tools installed.
I found the answer myself. Apparently the overloaded operators like [] are not allowed since they are inlined, see this question for a better explanation on that.
Moreover, I don't know why did I put single quotes for the statement I wanted to evaluate (I'm pretty sure I saw it in other place ... what do they actually mean in LLDB?) like so expr 'printf("Hey")'
So, taking out the quotes and using the answer in the cited question it suffices with something like
expr (int) myVector.__begin_[0]
to get the single value of a position in the vector.
Use p myVector or po myVector. These will print out the contents of your vector (alongside the size) in a couple of different formats.
To print a single value from the vector, you can use something like p (int)myVector[0].
I'm using VS2008 to write a program. There's one specific line in my code that causes a numerical error. It is:
Qp[j] = (Cp - Cm)/(Bp + Bm);
Qp is a std::vector. When I comment this line out, the numerical error disappears. I am going through my code line by line to find all the places that access Qp[j]. I was wondering if there was a feature in VS2008 or a linux program that wraps around the executable that can identify every line of code that reads from that section of memory (the specific element in the vector)?
I tried searching online but the keywords I used brought up results relating to global variables.
--- EDIT
Hi all. To those have responded, thank you. Just to clarify my question:
Imagine I have a vector with 5 elements. I'd like to know all the places in my code that use the value stored in element 3 at any point in time during execution. Is there an easy way to do this?
I am not sure if I understand you correctly, but if you comment out that line and the code works then maybe the problem is that line, and you don't need to check others lines.
Maybe in your case you get in the situation where Bp+Bm = 0 (division by zero error).
Qp may not have as many elements as the index j, check the size of Qp.
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.
I created a set of programs to calculate the area under a graph using various methods of approximation (midpoint, trapezoidal, simpson) for my Calculus class.
Here is an example of one of my programs (midpoint):
Prompt A,B,N
(A-B)/N->D
Input "Y1=", Y1
0->X
0->E
For(X,A+D/2,b-D/2,D)
Y1(x)+E->E
End
Disp E*D
Instead of applying these approximation rules to a function (Y1), I would like to apply them to a list of data (L1). How do I iterate through a list? I would need to be able to get the last index in the list in order for a "For Loop" to be any good. I can't do anything like L1.length like I would do in Java.
You can obtain the length of the list using dim(). That can be found in 2nd->LIST->OPS->dim(. Just make sure that you use a list variable otherwise dim() will complain about the type. You could then index into the list with a subscript.
e.g.,
{1, 2, 3, 4} -> L1
For (X, 1, dim(L1), 1)
Disp L1(X)
End
The for loop is the simplest way to iterate over a list in TI-Basic, as it is in many languages. Jeff Mercado already covered that, so I'll mention a few techniques that are powerful tools in specialized situation.
Mapping over lists
TI-Basic supports simple mapping operation over lists that have the same effect as a map function in any other language. TI-Basic support for this extends to most basic arithmetic function, and selection of other functions.
The syntax could not be simpler. If you want to add some number X to every element in some list L1 you type X+L1→L1.
seq(
Most for loops over a lists in TI-Basic can be replaced by cleverly constructed seq( command that will outperform the for loop in time and memory. The exceptions to this rule are loops that contain I/O or storing variables.
The syntax for this command can be quite confusing, so I recommend reading over this documentation before using it. In case that link dies, here's the most relevant information.
Command Summary
Creates a list by evaluating a formula with one variable taking on a
range of values, optionally skipping by a specified step.
Command Syntax
seq(formula, variable, start-value, end-value [, step])
Menu Location
While editing a program, press:
2nd LIST to enter the LIST menu RIGHT to enter the OPS submenu 5 to
choose seq(, or use arrows.
Calculator Compatibility
TI-83/84/+/SE
Token Size
1 byte
The documentation should do a good job explaining the syntax for seq(, so I'll just provide a sample use case.
If you want the square of every number between 1 and 100 you could do this
For Loop
DelVar L1100→dim(L1
for(A,1,100
A²→L1(A
End
or, this
seq
seq(A²,A,1,100→L1
The drawback of seq( is that you can't do any I/O or store any variables inside the expression.
Predefined list iteration function
Go to the LIST menu and check out all the operations under OPS and MATH. These predefined function are always going to be faster than a for loops or even a seq( expression designed to do the same thing.