I have this program which works fine. But if I comment out a cout statement (see code below) it throws the error. It tried this on ideone too with same result.
Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> ugly_numbers = vector<int>(n, 0);
ugly_numbers[0] = 1;
int ugly2 = 0, ugly3 = 0, ugly5 = 0, ugly_count = 1;
while (ugly_count <= n) {
int next_ugly_num_2 = ugly_numbers[ugly2] * 2;
int next_ugly_num_3 = ugly_numbers[ugly3] * 3;
int next_ugly_num_5 = ugly_numbers[ugly5] * 5;
int next_ugly_num = min(min(next_ugly_num_2, next_ugly_num_3), next_ugly_num_5);
if (next_ugly_num == next_ugly_num_2) ugly2++;
if (next_ugly_num == next_ugly_num_3) ugly3++;
if (next_ugly_num == next_ugly_num_5) ugly5++;
//cout << ugly_count << " - "; //*********THIS COUT IS WIERD**********
ugly_numbers[ugly_count] = next_ugly_num;
ugly_count++;
}
cout << ugly_numbers[n - 1] << endl;
return 0;
}
Error:
a.out: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
The terminal process "/bin/bash '-c', ' g++ -g Cpp/a.cpp -o a.out && clear && timeout 10 /usr/bin/time -v --output sys.txt ./a.out < input.txt > output.txt && rm *out'" terminated with exit code: 134.
Why is this happening? Why is this cout
statement affecting the behaviour of the program? And how do I correct it?