An easy way to watch a vector element in debugger

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


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.