I'm trying to allocate a vector<vector<Class>> which contain itself a vector<AnotherClass>, but I obtain an allocation error, so my question is: the max_size() given on a variable apply for all the vector of my program ?
Can I change this limit by changing my compiler ?
Here the code I used to check that :
class Couches
{
public:
Couches() : m_value(-1) {}
~Couches() {}
void initialize(const int& value) {
m_value = value;
}
private :
int m_value;
};
class Case
{
public :
Case() {}
~Case() {}
void initialize(const int& hauteur) {
m_couches.resize(hauteur);
for (int i(0); i<hauteur;i++)
m_couches[i].initialize(i);
}
private :
vector<Couches> m_couches;
};
void bug1()
{
vector<vector<Case>> m_cases;
m_cases.resize(5000, vector<Case>(5000));
cout<< m_cases.max_size()<<" " <<5000*5000*20<<endl;
for (int i(0); i<m_cases.size(); i++)
{
for (int j(0); j<m_cases[i].size(); j++)
{
m_cases[i][j].initialize(20);
}
}
}
I have a max_size of 357M < 500M I was expected to create.
EDIT : Sorry guys I said error but it's an error given by the debugger :
#1 0x405b36 operator new(unsigned int) () (??:??)
#2 0x490f58 typeinfo for std::time_put<wchar_t, std::ostreambuf_iterator<wchar_t, std::char_traits<wchar_t> > > () (??:??)
#3 0x4761ac std::allocator_traits<std::allocator<Couches> >::allocate(__a=..., __n=0) (D:/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include/c++/bits/alloc_traits.h:357)
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
I use an initialize function because it is a mcve and in my original code I need this function.
max_size()is implemented by the library implementor, it depends on the implementor whether or not you can change this.initializemethod, for your class, while having constructor empty? Since initialization of a class, is a job for constructor.