Pierre Chéneau
Pierre Chéneau

Reputation: 25

allocation error on C++ vector

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.

Upvotes: 2

Views: 3447

Answers (3)

Caleth
Caleth

Reputation: 62636

vector::max_size() relates to how big a single vector can get, in the absence of any other memory usage. None of your vectors approach that size. The biggest is 5000, with an individual allocation of 5000 * max(sizeof(vector<Case>), sizeof(Case)) which for me is 80,000.

The error you are seeing is that the total allocation of all the 25,000,000 Cases and 500,000,000 Couches exceeds the address space in your program

Upvotes: 1

geza
geza

Reputation: 29952

I suppose that you compiled your code in 32-bit.

Here's a guess, why get 341M as max_size. A typical implementation will get you the value of SIZE_MAX/sizeof(element_size). SIZE_MAX is 4GB-1. And you check the max_size() value for a vector which contains another vector. A typical sizeof(vector) is 12. So, the given answer is 4GB/12=341M. Note: the implementation can provide any max_size value it wants.

Compile your code in 64-bit, and if you have the necessary memory, your code will run.

Note: in your code, you don't have to worry about max_size(), as your vectors don't contain that much elements. I mean, there is no single vector instance, which has that much elements. The problem is that your summed memory consumption is large, which doesn't fit into ~2GB, which is a typical maximum allowed process size in 32-bit. So I think your program gets out of memory, there is no problem about vector size.

Note2: I've used M=2^20, G=2^30

Upvotes: 1

Ulug Toprak
Ulug Toprak

Reputation: 1212

max_size() is the maximum number of items that can be placed in a vector this number is limited by your system bits. if you are on a 32 bit system that is 2^32 char values. you are reaching the system or library implementation limitations thats why you get 375M

You should use std::vector<T>::size_type for your array index

Upvotes: 1

Related Questions