0

I'm trying to debug C++ code in VS Code with GDB. But here I'm unable to add an element to an array.

I even tried add element using below code

for(int i = 0; i < n ; i++){
  arr[i] = i;
}

Which ideally should give array of element from 0, 1, 2, 3, 4, 5.

Below is the snippet from my debugger.

enter image description here

My launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.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:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

Maqsud
  • 739
  • 3
  • 12
  • 35

1 Answers1

1

I'm not sure what's the actual problem but maybe my implementation helps you to figure what's wrong with yours:

#include <iostream>
#include <vector>

using namespace std;

inline int query(size_t n, int k) {
    std::vector<int> arr;
    arr.reserve(n);
    for (size_t i = 0; i < n; ++i) {
        int val;
        std::cin >> val;
        arr.push_back(val);
    }
    for (size_t j = 0; j < n; ++j) {
        if (arr[j] < k) {
            return arr[j];
        }
    }
    return 0;
} 

int main(int, char**){
    std::cout << query(3, 5);
    
    return 0;
}

You might also want to consult the documentation on cppreference: https://en.cppreference.com/w/cpp/container/vector/vector

kiloalphaindia
  • 561
  • 3
  • 7