C++ debugger ignoring breakpoints, but breaking on std::sort()

Viewed 48

I was working on a problem on Leetcode, and wanted to debug a weird looking WA. I brought my code over to VSCode as "seamlessly" as I could, hence the weird formatting for my code. For starters, my code is spitting out a completely different answer than on Leetcode. If that wasn't annoying enough, when I go to debug, my debugger ignores my breakpoints, and only breaks on std::sort(), as well as some of its header file calls. I'm using GDB.

I'm aware that my solution sucks, and I'm also aware of the far easier and more efficient solution right beneath my nose, but that isn't the focus of the post.

Here's my code:

#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    int binSearch(vector<int*> nums, int t) {
        int l = 0, r = nums.size(), m = 0;
        
            while (l < r) {
            m = floor((l+r)/2);
            if (*nums[m] > t) {
                r = m;
            } else {
                l = m+1;
            }
        }
        return r-1;
    }
    
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int*> copy;
        int ans[2];
        
        for (int i = 0; i < nums.size(); i++) {
            copy.push_back(&nums[i]);
        }
        
        sort(copy.begin(), copy.end(), [](int* a, int* b) { return *a < *b; });
        for (auto j : copy) {
            cout << *j;
        }
        
        for (int i = 0; i < copy.size(); i++) {
            int index = binSearch(copy, target - *copy[i]);
            if (index < 0) { continue; }
            if (nums[index] == target - *copy[i]) {
                ans[0] = distance(&nums[0], copy[i]);
                ans[1] = distance(&nums[0], copy[index]);
                break;
            }
        }
        
        vector<int> out(ans, ans+2);
        return out;
    }
};

int main() {
    vector<int> v{0,4,3,0};
    Solution sol;

    vector<int> ans = sol.twoSum(v, 0);

    cout << ans[0] << endl << ans[1];

    return 0;
}

Leetcode Output: [752,24624] //this is probably the random values assigned to an uninitialised arr
VSCode Output: 0

Here's my C++ launch task:

{
            "name": "C/C++: g++.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "targetArchitecture": "x86_64",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            /*"logging": {
                "moduleLoad": true,
                "programOutput": true,
                "trace": true,
                "traceResponse": true,
                "engineLogging": true
            },*/
            "preLaunchTask": "C/C++: g++.exe build active file"
        },

Additionally, the path to the file has no non-ASCII characters, and I did compile with -g

0 Answers
Related