Viewing dynamically alloocated null-terminated strings with Visual Studio's debugger

回眸只為那壹抹淺笑 提交于 2020-01-21 08:16:46

问题


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:


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.



来源:https://stackoverflow.com/questions/19363357/viewing-dynamically-alloocated-null-terminated-strings-with-visual-studios-debu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!