Reputation: 817
I have a Problem. I have a huge c++-project that I change at a few points to meet my requirements. I load more data than expected and at some point in this program there is a new vector allocated with the size of the number of data multiplied by another number.
vector = new real[data.size()*28];
Here I get the error message:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
because I can't allocate that much space . I can't change this vector because it is used in many different parts in the program and it would be very difficult and (for me) maybe impossible to fit the rest of the program to a new definition here.
What can I do that I can use this vector but get my large dataset into it?
Btw: I use eclipse, maybe I can increase the size of possible space to allocate in eclipse itself?
Thank you!
Upvotes: 1
Views: 1080
Reputation: 1835
As Encryptyon pointed out (and he should get the credit), you need to allocate your memory as a non-contiguous block. You can do this using a std::deque
.
std::deque<float> v( data.size() * 28 );
You can access any member using the operator[]
.
real x = v[1000000];
You van also iterate over (parts of the) deque, as if it was a std::vector
as the interface of a std::deque
is very similar to a std::vector
. However, what you cannot do, is &v[0]
(or v.data()
in c++11) since the internal storage of the container most probably is non-contiguous.
Upvotes: 2