Boost :: stable_vector member function does not return allocated capacity

Consider the following code.

#include <string> #include <boost/container/stable_vector.hpp> #include <iostream> int main() { boost::container::stable_vector<std::string> vec; vec.reserve(10); std::cout << "capacity = " << vec.capacity() << '\n'; } 

When running this (in g ++ / Linux) the output is:

capacity = 4294967286 (which is 2 ^ 32 - 10)

If I replaced boost :: container :: stable_vector with std :: vector above, the output would be as follows:

capacity = 10

I know this could be capacity = 20, or capacity = 64, or something else, but this is still normal behavior.

What bandwidth () returns for stable_vector, it seems (2 ^ 32 - N), N is the bandwidth requested with reserve (). I have not seen such a definition of capacity in the docs: http://www.boost.org/doc/libs/1_56_0/doc/html/boost/container/stable_vector.html#idp33067968-bb .

+7
c ++ boost stl
source share
1 answer

This is an obvious mistake. Criminal is this diff that changed this line to capacity()

 return (index_size ? (index_size - ExtraPointers + extra_capacity) : index_size); 

to

 const size_type index_offset = (ExtraPointers + extra_capacity) & (size_type(0u) - size_type(index_size != 0)); return index_size - index_offset; 

which was intended as "optimization" , presumably avoiding the branch.

Unfortunately, two blocks of code are not equivalent. The second option is actually equivalent

 return (index_size ? (index_size - (ExtraPointers + extra_capacity)) : index_size); // ^ ^ 

So, instead of adding extra_capacity (which is 10 in your case), it subtracts it.

The bug was fixed in the boot of Boost.Container, and the fix should be in the next version of Boost.

+10
source share

All Articles