Reputation: 471
I'm trying to reserve space for a vector of vectors, but it doesn't work and throws the following error:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Every time I use a large enough number. A minimal version of what I have is below:
#include <vector>
#include <iostream>
using namespace std;
int main(){
int base;
cout << "Enter Base: ";
cin >> base;
int dimension;
cout << "Enter Dimension: ";
cin >> dimension;
int perms = 1;
for(int i=0; i<dimension; i++){
perms *= base;
} // This gets the number of permutations with repetition
int length;
cout << "Enter Length: ";
cin >> length;
float structSize = 1.0;
for(float i=0.0; i<length; i++){
structSize *= perms-i;
structSize /= (i+1.0);
} // This gets the number of combinations
vector< vector< vector<double> > > allStructs;
allStructs.reserve(structSize);
return 0;
}
It should work for large structSizes, but fails at base=3, dimension=4, length=6 which makes structSize=324,540,216. Is it possible for this to work?
Upvotes: 1
Views: 904
Reputation: 20396
You need to think about your memory use.
It should work for large structSizes, but fails at base=3, dimension=4, length=6 which makes structSize=324,540,216. Is it possible for this to work?
So what you're doing, at an abstract level, is allocating a data structure that contains 324,540,216
instances of a vector<vector<double>>
object.
Here's what we know about vector
objects:
vector<double>
object, it's going to consume another [at-least-] 16 bytes each time you create one.So just on the face of it, your allStructs.reserve(structSize)
call is allocating 5 Gigabytes. It's probably allocating more than that, since the size of a vector's metadata could very well be larger than 16 bytes.
Upvotes: 5
Reputation: 9397
Declare your StructSize as double StructSize = 1.0;
then it should "logically" work.
However, reserve()
might not work due to possible memory limitations on your PC.
Upvotes: 2