I am trying to allocate a vector<bool>
in c++ for 50,000,000,000 entries; however, the program errors out.terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc (or in the online compiler it just ends).
I initially thought this was due to too large a size; however, v1.maxsize()
is greater than 50GB for me. What's confusing though, is when I reduce the # of entries it works fine.
Question: What could be the root cause of this considering that the number of entries is less than maxsize of a vector?
Other questions/answers have suggested that similar issues are due to being on a 32 bit cpu; however I have a 64bit.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
long size = 50000000000;
std::vector<bool> v1;
std::cout << "max_size: " << bool(v1.max_size() > 50000000000) <<"vs" << size << "\n";
v1 = std::vector<bool>(size,false);
cout << "vector initialised \n" << endl;
cout << v1.size() << endl;
}
note: I am essentially trying to create a memory efficient bitmap to track if certain addresses for a different data structure have been initialized. I cant use a bitset mentioned in this post since the size isn't known at compile time.