3

Is there any way to change the default behavior of Visual Studio's debugger such that when hovering over a null-terminated, dynamically allocated character array (C++), it will display the full content of the string, rather than the first character only?

I should mention that I am using Visual Studio 2010. If there is a way to achieve this in VS2012 only though, I would be interested to know that as well!

Dan M. Katz
  • 337
  • 2
  • 13

1 Answers1

4

There's a useful link for visual studio, C++ Debugger Tips:

To interpret a pointer expression as a string, you can use ‘,s’ for an simple null-terminated string, ‘,s8‘ for a UTF-8 string, or ‘,su‘ for a Unicode string. (Note that the expression has to be a pointer type for this to work).

For example you break in the following function

void function(char* s)
{
   // break here
}

in the MSVC watch window (or debugger), you would first try to just add s but it will only display the first character. But with the above information, you could append the following suffixes to the variables in the watch window:

s,s8

or if you know it's unicode, try:

s,su

This even works for arbitrary pointers, or say for other data types, e.g. debugging the content of a QString:

QString str("Test");
// break here

For this, possible watch window (or debugger) statements are:

((str).d)->array,su                 <-- debug QString (Qt4) as unicode char string
(char*)str.d + str.d->offset,su     <-- debug QString (Qt5) as unicode char string
0x0c5eae82,su                       <-- debug any memory location as unicode char string

If appending ,s8 or, respectively ,su does not work, try the other variant.

DomTomCat
  • 8,189
  • 1
  • 49
  • 64
  • This is handy, but how can I edit chars in a string not by editing individual chars? It's 2020 now. – vehsakul Jul 28 '20 at 12:19
  • @vehsakul Why're you telling me it's 2020? I sure know that. If you're annoyed please turn to the VS or Qt developer team (and please be nice). Here I've suggested a workaround - and this has been years ago btw. – DomTomCat Jul 30 '20 at 10:07
  • Well, I left my comment with a followup question just in case you or somebody else knows the answer. It's supposed to be neutral. I mentioned 2020 because I think that if there is no such feature in VS now, then this is sad. – vehsakul Jul 30 '20 at 17:32
  • @DomTomCat: Is it possible to use this technique to create conditional breakpoints? For example, break in a line if the content of a `QString` variable is equal to `"Test"`. – Baumann Apr 27 '21 at 18:41