gdb:调试VS代码时数组中的<错误读取变量>

Posted

技术标签:

【中文标题】gdb:调试VS代码时数组中的<错误读取变量>【英文标题】:gdb: <error reading variable> in array while debugging VS code 【发布时间】:2021-02-27 03:06:15 【问题描述】:

在调试期间尝试查看数组的内容时遇到问题。而不是我看到的字符。

我的代码:

#include <stdio.h>

int main()

    char str[100] = 0;
    fgets(str, 100, stdin);

    return 0;

我在VARIABLES 窗口中看到的内容:

但是当我尝试调试 int 数组时,一切正常。

示例:

#include <stdio.h>

int main()

    int str[100];
    for (int i = 0; i < 100; i++)
    
        str[i] = i+1;
    
    

    return 0;

我所看到的:

tasks.json:


    "version": "2.0.0",
    "tasks": [
        
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
            "args": [
                "-g",
                "$file",
                "-o",
                "$fileDirname\\$fileBasenameNoExtension.exe"
            ],
            "options": 
                "cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
            ,
            "problemMatcher": [
                "$gcc"
            ],
            "group": 
                "kind": "build",
                "isDefault": true
            ,
            "detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe\""
        
    ]

launch.json:


    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
            "name": "gcc.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "$fileDirname\\$fileBasenameNoExtension.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "$workspaceFolder",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                
            ],
            "preLaunchTask": "C/C++: gcc.exe build active file"
        
    ]

如何查看数组中的字符?

【问题讨论】:

我也遇到了同样的问题,你找到解决办法了吗? 【参考方案1】:

因为漂亮的打印机没有正确显示 utf-8 字符。要显示 utf-8 字符,请编辑 launch.json 中的 setupCommands:

  "setupCommands": [
    
      "description": "Enable pretty-printing for gdb",
      "text": "-enable-pretty-printing",
      "ignoreFailures": true
    ,
    
      "description": "Fix pretty-printing for gdb",
      "text": "set charset UTF-8"
    
  ],

原答案:VSCode debugger having issues with character encoding

【讨论】:

以上是关于gdb:调试VS代码时数组中的<错误读取变量>的主要内容,如果未能解决你的问题,请参考以下文章

使用Windows 10和MINGW编译器在VS Code上使用gdb调试器调试时出错

VS2010 监视变量变化

使用gdb进行写操作

GDB调试字符数组时指针和数组区别的体现

vs2012调试时怎么查看堆中的数据(动态数组)? 求详细解答。 用的c++

有没有一种方法可以在100到1000个索引的范围内打印长数组到GDB中的txt文件? (调试Fortran代码)