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

后端 未结 1 850
一生所求
一生所求 2021-01-14 12:22

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

相关标签:
1条回答
  • 2021-01-14 12:55

    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.

    0 讨论(0)
提交回复
热议问题