Topic: should std::vector<> exponential growth rate be followed strictly


Author: ron@sensor.com (Ron Natalie)
Date: Mon, 25 Oct 2004 19:50:16 GMT
Raw View
Dhruv Matani wrote:
> Hello,
>  I recently had a discussion about whether the exponential growth policy
> for std::vector<> should be followed strictly no matter what.

The standard doesn't mandate any particular policy other than that
the amortized cost for extending the array size be constant.

If you want to roll off the expansion as the memory gets closer
to some known limit, that would be legitimate.

>

---
[ 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                       ]





Author: chris@bubblescope.net (chris)
Date: Tue, 26 Oct 2004 21:18:18 GMT
Raw View
Dhruv Matani wrote:
> 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');
>
..
> Assume that the growth factor is 2.
..
> 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 ;-)
If your only aim is to remain standards compliant, then one option would
be to reduce what you claim the minimum growth factor will be in tight
memory situations, say to 1.05. If you can't extend the vector by at
least 5% then is it really worth doing it? In this way you could perform
better in low memory situations but still be standard compliant.

If you wanted to be really sick, you could claim that your minimum
growth factor is 1+5*10^(-20), which would mean that on a system with a
64-bit address space you would still never have to expand by more than
1, but I believe you could still claim to be standards compliant ;)

Chris

---
[ 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                       ]