Problem p00222 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
#define all(n) n.begin(), n.end()
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define INF (1 << 27)
#define EPS 1e-10
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define LIM 10000010
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef unsigned long long ull;
typedef long long ll;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
bool isprime[LIM];
vector<int> Quadruplet;

void sieve() {
  for (int i = 0; i <= LIM; i++)
    isprime[i] = true;
  isprime[0] = isprime[1] = false;
  int Size = 0;
  for (int i = 2; i < LIM; i++) {
    if (isprime[i]) {
      // prime.pb(i);
      Size++;
      for (int j = 2 * i; j < LIM; j += i)
        isprime[j] = false;
    }
  }

  for (int i = 0; i < LIM; i++) {
    if (i + 8 <= LIM && isprime[i] && isprime[i + 2] && isprime[i + 6] &&
        isprime[i + 8]) {
      Quadruplet.pb(i + 8);
    }
  }
}

int main() {
  clock_t start, end;
  // start = clock();

  sieve();

  // end = clock();

  // cout << setiosflags(ios::fixed) << setprecision(10) <<
  // (double)(end-start)/CLOCKS_PER_SEC << endl;

  int n;
  while (cin >> n && n) {
    cout << (upper_bound(all(Quadruplet), n)[-1]) << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
#define all(n) n.begin(), n.end()
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define INF (1 << 27)
#define EPS 1e-10
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define LIM 10000000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef unsigned long long ull;
typedef long long ll;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
vector<int> prime;
bool isprime[LIM];
vector<int> Quadruplet;

void sieve() {
  for (int i = 0; i <= LIM; i++)
    isprime[i] = true;
  isprime[0] = isprime[1] = false;
  int Size = 0;
  for (int i = 2; i < LIM; i++) {
    if (isprime[i]) {
      // prime.pb(i);
      Size++;
      for (int j = 2 * i; j < LIM; j += i)
        isprime[j] = false;
    }
  }

  for (int i = 0; i < Size; i++) {
    if (i + 8 < LIM && isprime[i] && isprime[i + 2] && isprime[i + 6] &&
        isprime[i + 8]) {
      Quadruplet.pb(i + 8);
    }
  }
}

int main() {
  clock_t start, end;
  // start = clock();

  sieve();

  // end = clock();

  // cout << setiosflags(ios::fixed) << setprecision(10) <<
  // (double)(end-start)/CLOCKS_PER_SEC << endl;

  int n;
  while (cin >> n && n) {
    cout << (upper_bound(all(Quadruplet), n)[-1]) << endl;
  }
  return 0;
}