Containers like std::basic_string
and std::vector
perform automatic re-allocations when internal capacity runs out. The standard specifies that, after a re-allocation, .capacity() >= .size()
.
What are some of the actual multipliers used by mainstream toolchains when performing re-allocations?
Update
So far, I have:
Dinkumware: 1.5 (ships with MSVS and possibly ICC)
GNU libstdc++: 2 (ships with GCC and possibly ICC)
RW/Apache stdcxx: 1.618 (aka φ)
STLport: 2
New answer for an old question.
Rationale: The answer can be answered programatically, and with online compilers, relatively easily. Here is a program that can help you answer this question:
#include <climits>
#include <cstddef>
#include <cstdlib>
#ifndef _MSC_VER
# include <cxxabi.h>
#endif
#include <iostream>
#include <memory>
#include <string>
#include <typeinfo>
#include <type_traits>
#include <limits>
#include <vector>
#include <string>
template <typename T>
std::string
type_name()
{
typedef typename std::remove_reference<T>::type TR;
std::unique_ptr<char, void(*)(void*)> own
(
#ifndef _MSC_VER
abi::__cxa_demangle(typeid(TR).name(), nullptr,
nullptr, nullptr),
#else
nullptr,
#endif
std::free
);
std::string r = own != nullptr ? own.get() : typeid(TR).name();
if (std::is_const<TR>::value)
r += " const";
if (std::is_volatile<TR>::value)
r += " volatile";
if (std::is_lvalue_reference<T>::value)
r += "&";
else if (std::is_rvalue_reference<T>::value)
r += "&&";
return r;
}
template <class C>
void
test()
{
C c;
std::cout << type_name<C>() << ":\n";
std::size_t c0 = c.capacity();
std::cout << " Initial capacity is " << c0 << '\n';
c.resize(c0);
for (int i = 0; i < 10; ++i)
{
c.push_back(typename C::value_type{});
std::size_t c1 = c.capacity();
if (c0 != 0)
{
float f = static_cast<float>(c1)/c0;
std::cout << " growth factor appears to be " << f << '\n';
}
c0 = c1;
c.resize(c0);
}
}
int
main()
{
test<std::vector<int>>();
test<std::string>();
}
Most of the complexity is a bit unnecessary as it is just to get type_name
working.
libstdc++:
http://melpon.org/wandbox/permlink/njaIG2uiR2vlCLZz
appears to answer a solid 2 for both vector and string.
VS:
http://webcompiler.cloudapp.net
is very close to 1.5 for both vector and string.
libc++
http://melpon.org/wandbox/permlink/mXshrLJHgNuvE1mD
is very close to 2 for both vector and string.
Note that this program also tells you what the short string buffer is for string
: 15 for both libstdc++ and VS, and 22 for libc++.
Dinkumware STL (shipped with Visual Studio) uses a 1.5 multiplier, Gcc uses 2. I cannot really tell for the rest, but I think those are the most often used numbers. (IIRC, I read once that most implementations used 2)
As a side comment, you are correctly calling it a multiplier since the standard requires the growth to be (at least) geometrical.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With