VSCode调试器卡死

3
我正在尝试在VSCode中调试一个C++文件。在main()函数中,调试器可以正常工作,但是一旦进入一个函数,调试器就会冻结,并且控制面板不再起作用(除了停止按钮)。在左侧栏中,“本地”变量旁边显示加载图标,一直处于加载状态。
我使用的操作系统是macOS 11.3.1,VSCode版本为1.58.2(通用版)。
以下是我的文件:debug.cpp
#include<iostream> 
#include<queue> 
#include<vector>
#include<map> 
#include<stack> 
#include<unordered_map>
#include<string>
using namespace std;

int longestConsecutive(vector<int>& nums) {

    unordered_map<int, bool> hash;
    for(int i = 0; i<nums.size(); i++){
        hash[nums[i]] = true;
    }

    int max_range = 0;
    for(int i = 1; i<nums.size()-1; i++){
        if(hash[nums[i]]){
            hash[nums[i]] = false;
            int j = i-1;
            int leftRange = 0;
            while(hash[nums[j]] && j>=0){
                hash[nums[j]] = false;
                leftRange++;
                j--;
            }
            int k = i+1;
            int rightRange = 0;
            while(hash[nums[k]] && k<nums.size()){
                hash[nums[k]] = false;
                rightRange++;
                k++;
            }
            if(leftRange+rightRange+1 > max_range){
                max_range = leftRange+rightRange+1;
            }
        }
    }
    return max_range;

}

int main(){
    vector<int> vec;
    vec.push_back(100);
    vec.push_back(4);
    vec.push_back(200);
    vec.push_back(1);
    vec.push_back(3);
    vec.push_back(2);

    int ans = longestConsecutive(vec);

    cout<<ans<<endl;
}

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": "clang++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang++ build active file"
        }
    ]
}

需要注意的一点是,当我在debug.cpp中更改我的longestConsecutive函数,并使其变成完全随机的东西时:

int temp;
temp = 100;
return temp;

那么调试器就能正常工作了。我写的代码有什么问题吗?

提前感谢!


1
所有的反向范围检查都应该修复,以免引入未定义行为(UB)。例如:while(hash[nums[j]] && j>=0) 是反向的。应该改成 while(j>=0 && hash[nums[j]])。类似问题出现在这里:while (hash[nums[k]] && k<nums.size())。只有当第一个表达式为真时,才应该考虑后面的表达式,并且第一个表达式应该始终验证后面的表达式是否可以被评估而不会产生UB。这在您的程序中发生了多次。关于 size_tint 的符号/无符号用法的编译器警告也值得考虑。 - WhozCraig
1
不相关的内容:CodeLLDB是一个使用clang和lldb的杰出的MacOS调试扩展,我强烈建议它值得一看。 - WhozCraig
1
我会尝试使用CodeLLDB,而且我不知道范围检查应该在其他检查之前完成。我会尽快修复它们!:D - Aditya Prakash
为什么需要将“hash”变量设置为“map”类型? - rioV8
@rioV8 解决这个 leetcode问题。我的逻辑是将给定数组中的所有数字映射为true,然后从哈希表的第一个索引开始迭代。我将检查每个索引i,如果hash[i-1]存在,则将范围计数器增加一,并将其映射值设置为false,因为我已经在范围内考虑过它了。如果没有,则在i的右侧重复此过程,最后将当前范围与max_range进行比较。这个问题能以更好的方式解决吗? - Aditya Prakash
显示剩余4条评论
1个回答

2

我之前也遇到过这种情况。只需在 launch.json 中将 [type] 的配置从 "cppdbg" 改为 "lldb",即可解决此问题。希望能对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接