I'm trying to make a huge collection of random numbers to do algorithm analysis. Then I came across this problem that I can't explain. Consider the following code:
#include <exception>
#include <iostream>
#include <deque>
typedef unsigned long long mytype;
const mytype SIZE = 150000000;
int main()
{
std::deque<mytype> rand;
try
{
for (mytype i = 0; i< SIZE; i++)
{
rand.push_back(1); //Just push a dummy number into the deque
}
}
catch (std::exception& e)
{
{
std::cout << e.what() << std::endl;
}
}
return 0;
}
This will end up with a bad allocation exception. The thing is if I use vector and reserve() it will work. Correct me if I'm not getting this right, isn't deque the kind of data structure that have larger capacity, since its not allocating memory continuously like vector?
I'm running this on Win8 x64, visual studio 2012, intel i7 with 8G RAM. Thanks for sharing your thoughts