Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 993
Paul Jurczak Avatar asked Aug 11 '14 18:08

Paul Jurczak


People also ask

How do you access a specific element in a vector?

Vector elements are accessed using indexing vectors, which can be numeric, character or logical vectors. You can access an individual element of a vector by its position (or "index"), indicated using square brackets. In R, the first element has an index of 1.

How do I view an array in debugger?

Starting with Visual C++ version 6.0, it's now possible to expand an array pointer to view all array elements in the Visual C++ debugger Watch window. This feature isn't documented. In the Watch window, type an expression that evaluates to a pointer followed by a comma and the number of elements in the array.

How do you retrieve an element from a vector?

get() method is used to fetch or retrieve an element at a specific index from a Vector.

How do you find the elements in a vector array?

Use std::find_if() with std::distance() This is a recommended approach to finding an element in a vector if the search needs to satisfy a specific logic eg, finding an index of element in the vector that is a prime number using prime number logic.


2 Answers

Just prefix each [] with _Myfirst in the Watch field:

YourVector._Myfirst[n]
like image 123
feos Avatar answered Sep 17 '22 19:09

feos


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:

  1. Add the variable to the watch windows.
  2. Add ,! after the name of the variable (ex: v,!) this indicate VS that you want to turn off debugger visualization.
  3. 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.
  4. Erase v,! from the watch if you want.
  5. 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.
like image 30
NetVipeC Avatar answered Sep 16 '22 19:09

NetVipeC