6

I've got a custom data structure holding a char* buffer with two lengths associated: maximum and actual length:

struct MyData {
  char* data;
  int length;
  int capacity;
};

In the Visual Studio (2015) debugger visualizer I only want to display the first length elements of the data buffer and not the (usually uninitialized) remaining elements.

I've the following rule in my custom .natvis file for displaying my custom data structure:

<Type Name="MyData">
  <DisplayString>content="{data,su}" length={length}</DisplayString>
</Type>

Is it possible to only display data as a "su"-encoded string from data[0] to data[length-1]?

Torbjörn
  • 5,512
  • 7
  • 46
  • 73
  • if `data[length]` is `'\0'` then most debuggers won't show anything beoind the \0. – Paul Ogilvie Apr 27 '16 at 08:00
  • Unfortunately, this legacy code I'm working on hasn't always \0 terminated strings. I know, a little scary. – Torbjörn Apr 27 '16 at 09:38
  • Then I don't see how a debugger is to know the length-in-use is `length`. What you can do is, when allocating the memory, zero it (use `calloc` ot `memset`) so there will be a `\0` (unless `data` is re-used with different lengths in use). – Paul Ogilvie Apr 27 '16 at 14:45
  • I know that the member variable `length` is the correct length of used/occupied elements in `data` and I want to display only those. Something in the form of an pseudo-expression of `content={data[0..length-1]}`. – Torbjörn Apr 27 '16 at 17:30

1 Answers1

14

This will limit the length of the string in the debugger:

<Type Name="MyData">
    <DisplayString>{data,[length]su}</DisplayString>
</Type>
Trasig Torsk
  • 141
  • 3
  • Explained here: [Format specifiers for C++ in the Visual Studio debugger](https://learn.microsoft.com/en-us/visualstudio/debugger/format-specifiers-in-cpp?view=vs-2017) – Andreas Haferburg Mar 24 '19 at 16:55