-2

I have written code to test how many of the same numbers are present in both arrays, but for some reason it throws 'std::bad_alloc' could anyone explain why? It only throws it when N = 1000000 for some reason, is it because I have allocated 4000000 bytes of memory?

Here's the code:

#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <math.h>
#include <iomanip>

using namespace std;

int find_last_before_zero(const vector<int>& vec) {
 for (int i = vec.size() - 1; i >= 0; --i) {
    if (vec[i] != 0) return i + 1;
 }
return vec.size();
}

void gen_random_array(vector<int>& vec){
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dist(100000, 100200);

for(int i = 0; i < vec.size(); ++i){
    vec[i] = dist(gen);
}
}

void binSearchClient(int T){
int counters[4] = {0,0,0,0};
string names[4] = {"for n = 1000 = ", "for n = 10000 = ", "for n = 100000 = ", "for n = 1000000 = "};
for(int i = 0; i < T; ++i){
    int N = 1000;
    for(int k = 0; k < 4; ++k){
        N *= pow(10.0, k);
        vector<int> first(N), second(N);
        gen_random_array(first, N); gen_random_array(second, N);
        vector<int> intersection(N);
        sort(first.begin(), first.end()); sort(second.begin(), second.end());
        set_intersection(first.begin(), first.end(), second.begin(), second.end(), intersection.begin());
        counters[k] += find_last_before_zero(intersection);
            }
        }
    }
}
for(int i = 0; i < 4; ++i){
    cout << names[i] << setprecision(10) << std::fixed << (1.0 * counters[i]) / T << endl;
}
}

int main(){
binSearchClient(1);
}
Pavel
  • 1
  • 3
  • 17
  • 51
  • When you searched for [std::bad_alloc](http://en.cppreference.com/w/cpp/memory/new/bad_alloc), what did you find? What platform are you running on, would you expect an allocation of that size to succeed there, and did you try allocating the same amount a different way for comparison? – Useless Apr 16 '15 at 14:37
  • `std::bad_alloc` means memory allocation failed. That usually happens when you run out of memory or at least, don't have enough continuous memory for a large allocation. *Are* you allocating too much memory? How much free memory do you have? On which line is the exception thrown? – eerorika Apr 16 '15 at 14:38
  • I'm certain you could narrow this down further. – Lightness Races in Orbit Apr 16 '15 at 14:55
  • 1
    @JonathanWakely my visual studio 2010 was not telling me why and what was throwing bad_alloc. So no, it wouldn't have been quicker. – Pavel Apr 16 '15 at 16:48

1 Answers1

4

In you first iteration you have N as 1000. Then in for(int k = 0; k < 4; ++k) you do N *= pow(10.0, k);. So for the first iteration N = 1000 (N(1000) * 10^0) The k becomes 1 and you N = 10000 (N(1000) * 10^1). Then k becomes 2 and N = 1,000,000 (N(10000) * 10^2). At k = 3 you get N = 1,000,000,000,000,000 (N(1,000,000) * 10^3) which is more than likely memory than you can allocate.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402