9

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72

4 Answers4

9

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

YourVector._Myfirst[n]
feos
  • 1,099
  • 1
  • 9
  • 19
  • 1
    `_Myfirst` didn't work in Visual Studio 2019 (I didn't expected it to, the answer is quite old). I used `__begin_` like this `YourVector.__begin_[index]` – Bruno Bieri Aug 28 '21 at 06:38
3

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.
NetVipeC
  • 4,402
  • 1
  • 17
  • 19
  • Thanks for detailed response, but I'm looking for much quicker way of accomplishing it - see my question. I have many large vectors and many complex expressions involving vector elements. – Paul Jurczak Aug 11 '14 at 20:00
  • Once you have one added the watch, the other are easily, copying the base address of the vector, changing the variable name `v` to the vector in question, the `offset` and `elements` needed. You don't have to do the 5 step process all the time. The tips here are, `,!` trick and `,count` trick. Other possible way to go are [Debugger Visualizations](http://blogs.msdn.com/b/vcblog/archive/2013/06/28/using-visual-studio-2013-to-write-maintainable-native-visualizations-natvis.aspx), don't known much of it, only the basics. In other debugger (ex: lldb) you could do the visualization in python. – NetVipeC Aug 11 '14 at 20:46
0

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]
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Hossein
  • 111
  • 1
  • 7
0

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.

vincent thorpe
  • 171
  • 1
  • 10