0

I wrote a c++ program but I am getting an error. I am not understanding the reason why I am getting the error. Please help me with how to fix this.

This is my code :

#include <bits/stdc++.h>
#define endl "\n"
#define int long long
using namespace std;

void solve() {
    int n, x;
    cin >> n >> x;
    cout << n << x;
    vector <int> in(n);
    for (auto &p : in)
        cin >> p;

    vector<int> vect;
    vect = in;

    int cnt = 0;
    int l = n;
    for (int i = 0; i < l; i++) {
        if (vect[i] % 2 == 0) {
            while (x--) {
                vect.push_back(vect[i] / 2);
                l++;
            }
        }
        else {
            cnt = i;
            break;
        }
    }
    int sum = 0;
    for (int i = 0; i < cnt; i++) {
        sum += vect[i];
    }
    cout << sum << endl;
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

The input is :

2 1 2 12 4 2 4 6 8 2 

This is the error which I am getting on compiling it in sublime text :

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
[Finished in 4.0s]
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
rgv
  • 61
  • 10
  • `bad_alloc` is an exception, you don't get that error at compile time. It happens during execution. What input are you giving your program? – François Andrieux Jan 05 '21 at 16:31
  • 5
    `#define endl "\n"` and `#define int long long` are not allowed in C++ if you include any standard headers. – François Andrieux Jan 05 '21 at 16:31
  • 3
    Please read [Why should I not #include ?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – François Andrieux Jan 05 '21 at 16:32
  • 1
    This is the input that I am giving: 2 1 2 12 4 2 4 6 8 2 I changed the header files still I am getting the same error. – rgv Jan 05 '21 at 16:34
  • 1
    The bug could be here: `vector in(n);` you should always validate the input before using it. `n` could be a garbage value or an illegal value. – drescherjm Jan 05 '21 at 16:35
  • What is the reason for the error and how to fix the error? @drescherjm – rgv Jan 05 '21 at 16:37
  • 1
    I mentioned that in the edit. `n` could be a garbage value if the user entered text like "frog" instead of an integer. Or an invalid value if the user entered `-1` or something else that is negative. – drescherjm Jan 05 '21 at 16:37
  • 2
    `#define int long long` -- There is no need for this horrific stuff. There is an `int64_t` type in C++, a meansly 4 more characters to type in to describe fully the type and the number of bits the type holds. – PaulMcKenzie Jan 05 '21 at 16:44

1 Answers1

3

The problem is in the treatment of the variable x. Don't ask me what the solution is but I can describe the problem.

        while (x--) {
            vect.push_back(vect[i] / 2);
            l++;
        }

The first time this loop runs x equals two. So two items are added to the vector and x takes the value -1.

The next time this loop runs x starts at -1 and so the loop runs until you run out of memory and get a bad_alloc exception.

john
  • 85,011
  • 4
  • 57
  • 81
  • It seems likely the solution is to copy `x` before the loop and use the copy. Maybe converting the `while` loop to a `for` loop which initializes its iterating variable to `x` would be clearer. – François Andrieux Jan 05 '21 at 16:46
  • 1
    @FrançoisAndrieux yes using for loop would be better and the error was due to while(x--). – rgv Jan 05 '21 at 16:50
  • Not to mention the fact that the `while` loop increments `l`. `l` is the upper limit for `i` in the containing loop. – Peter Jan 05 '21 at 17:49