Topic: should std::vector<> exponential growth rate be followed strictlyin times of low availabe memory.


Author: dhruvbird@gmx.net ("Dhruv Matani")
Date: Mon, 25 Oct 2004 18:21:59 GMT
Raw View
Hello,
 I recently had a discussion about whether the exponential growth policy
for std::vector<> should be followed strictly no matter what.

The case I'm looking at is something like this:

const int init_sz = 1.2 * 1024 * 1024 * 1024; // 1.2GB.
std::vector<char> cv(init_sz);
cv.push_back('a');


Now, what a 'typical' vector implementation would do is:
Allocate space for init_sz chars, and default construct them.
When the push_back function is called, something along these lines happens
inside the vector:

Assume that the growth factor is 2.
1. Allocate space(memory) having size cv.capacity() * 2.
2. Copy the old contents into the newly allocated memory block.
3. Deallocate the old block.

Now, in this case, the vector had an old capacity of init_sz, and
2*init_sz would be such that init_sz+2*init_sz is > 3GB. Linux(32-bit)
supports a max of 3GB segments for the application's data, so clearly the
allocator would not be able to provide the required memory. What I'm now
thinking about is should the standard be permissive and allow the vector
to start growing slowly now, say increase the size by 10 elements to
prevent bad_alloc from being thrown?

What my argument is that an implementations should be allowed to deviate
slightly from the standard to prevent useless exception like the above
from being thrown. It should be clear that from the user's point of view,
what he sees is that he already has 1.2GB of data, and he is trying to add
1-byte to that! Why for any reason should that fail? Assume that the user
is in no position to use reserve. Obviously if that is considered, then
the whole post is useless ;-)


Reagrds,
-Dhruv.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]