I noticed that the string capacities in C++ follow this pattern:
- initial string size is 15
- for any string that is larger than a particular size 'block', the capacity is doubled.
Here are the string capacities for strings up to length 500:
15
30
60
120
240
480
960
Capacities were found with the following C++ program:
#include <iostream>
#include <vector>
using namespace std;
string getstr(int len) {
string s = "";
for (int i=0; i<len; i++) {
s.append("1");
}
return s;
}
int main() {
vector<int> capacities;
int prevcap;
for (int i=0; i<500; i++) {
int cap = getstr(i).capacity();
if (cap > prevcap) {
capacities.push_back(cap);
prevcap = cap;
}
}
for (int i : capacities) {
cout << i << endl;
}
}
What is the logic behind choosing this algorithm? Do the numbers (here 15 and 2) have any significance, or have they been randomly chosen? Also, does this algorithm vary from compiler to compiler? (This was compiled and tested with g++ 5.4.0 on Ubuntu 16.04) Any insights are appreciated.
clang 12.0.0
) I am getting :22 47 95 191 383 767
. I don't think there is any specific pattern here.gcc
seems to be just doubling the capacity each time from your observations. – Wyndham#include <string>
in there. – Alphitomancypush_back
– Dareenreserve()
to limit the allocation calls – Dendrite