Viewing dynamically alloocated null-terminated str

2019-02-19 23:24发布

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!

1条回答
唯我独甜
2楼-- · 2019-02-19 23:56

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.

查看更多
登录 后发表回答