infixed
infixed

Reputation: 1155

Exporting an extended std::vector dump in MSVS debugger

While debugging an issue, I would like to save a copy of a 6000+ sized std::vector so I can compare it from run to run.

Now the Microsoft Visual Studio debugger has an 'Immediate Window' where you can dump variables, including std::vector by entering a command like

>Debug.Print m_MyVector

( there is an alias for this command as a simple ? )

The problem is, is that it is limited. Try to dump a long vector and one gets output like < More... (The first 100 of 6529 items were displayed.) >

My problem is how to get the rest. If it was like the utility more and you could get it 100 at a time, that would be OK. Or if you could set the block to something more than 100 at a time.

While I could write a custom serializer and dump to a file, I don't have <iostream> included in this module yet, and I'd hate to start adding things like that just for debugging.

Any other ways to export large data objects out of the MSVS debugger?

Upvotes: 3

Views: 1337

Answers (1)

Cody Gray
Cody Gray

Reputation: 244913

Printing Values Piecemeal in the Immediate Window

If it was like the utility more and you could get it 100 at a time, that would be OK.

You can arrange for this by incrementing the pointer and reprinting. Unfortunately, operator overloads are not available in the Immediate Window, so you can't do something like ?(&m_MyVector[0] + 100). Instead, you have to refer to undocumented private members. For example:

?(m_MyVector._Myfirst), 100

    ...Prints first 100 items

?(m_MyVector._Myfirst + 100), 100

     ...Prints next 100 items

?(m_MyVector._Myfirst + 200), 100

     ...Prints next 100 items

…and so on. The , 100 syntax there tells it how many items to print. No, asking for more than 100 items doesn't work. You just get the original message:

< More... (The first 100 of n items were displayed.) >

Seeing the Big Picture in the Memory Window

Perhaps a better solution would involve the Memory window. This exploits the fact that std::vector is a contiguous container, so all of its elements will be stored adjacent to one another in memory. The Memory window just dumps the contents of memory, which will effectively give you an unlimited dump of the vector's contents.

  1. Use the Immediate Window to determine the actual address of the vector's first member:

    ?m_MyVector._Myfirst
    0x00440068
    
  2. Make sure that a Memory window is displayed. Debug → Windows → Memory → Memory 1 (there actually are 4 of them; it doesn't matter which one you use). Or just press Alt+6.

  3. Copy the address you got from the Immediate Window into the top of the Memory window, where it says "Address". Leave the 0x prefix there, just in case. Press Enter.

  4. The Memory window will refresh its view to show you the contents of memory at that address, and immediately following it. You can scroll down as far as you like. There is no 100-item limit.

Now, the only disadvantage of this is that the Memory window literally displays a hex dump of bytes. So if you have a vector of strings, you're not going to be nearly as happy as if you have a vector of ints, like I have here (filled sequentially, starting from 0):

Dumping to the Clipboard from Auto/Locals Window

The solution you mentioned in a comment will also work. Use the Autos or Locals window to expand the m_MyVector object. Do a Select All, or use Shift+Click at the top and bottom to exclude other unwanted items. Then Copy. If you have a particularly large vector, twiddle your thumbs for a good 30+ seconds while Visual Studio dumps all this data to your clipboard. Then paste into Notepad. After cleaning up the first few lines, you'll have a nice little dump file:

std::vector<int,std::allocator<int> >
        [size]  60000   unsigned long
        [capacity]  61447   unsigned long
        [0] 0   int
        [1] 1   int
        [2] 2   int
        [3] 3   int
        [4] 4   int
        [5] 5   int
        [6] 6   int
        [7] 7   int
        [8] 8   int
        [9] 9   int
        [10]    10  int
        ...

Upvotes: 3

Related Questions