4

Is the capacity size of string always a multiple value of 15?

for example: In all cases the capacity is 15

string s1 = "Hello";

string s2 = "Hi";

string s3 = "Hey";

or is it random?

5
  • 1
    It'll depend on your STL implementation - which compiler version / STL are you using? Commented Feb 17, 2011 at 17:52
  • FYI, A quick look at the STL code on VS2008 shows the initial size is 16; maybe the size is implementation specific, but I think there's always an initial capacity bigger than zero. Commented Feb 17, 2011 at 18:08
  • I'm on VS2005 VC++ 8.0 but recently I have updated to latest Windows SDK. Commented Feb 17, 2011 at 18:09
  • @Max: it's probably saving 1 for the ending \0 . Commented Feb 17, 2011 at 18:18
  • A good reference on this topic: learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity Commented Feb 17, 2011 at 19:41

3 Answers 3

9

Is the capacity size of string always a multiple value of 15?

No; the only guarantee about the capacity of a std::string is that s.capacity() >= s.size().

A good implementation will probably grow the capacity exponentially so that it doubles in size each time a reallocation of the underlying array is required. This is required for std::vector so that push_back can have amortized constant time complexity, but there is no such requirement for std::string.

In addition, a std::string implementation can perform small string optimizations where strings smaller than some number of characters are stored in the std::string object itself, not in a dynamically allocated array. This is useful because many strings are short and dynamic allocation can be expensive. Usually a small string optimization is performed if the number of bytes required to store the string is smaller than the number of bytes required to store the pointers into a dynamically allocated buffer.

Whether or not your particular implementation performs small string optimizations, I don't know.

Sign up to request clarification or add additional context in comments.

4 Comments

How can i find the std::string implementation detail?
@user3234: Find your implementation's <string> header and take a look. Is there a particular reason that you need to know or are you just curious?
I don't see anything in <string> header file. I need to append a character \n at the end of my std::string but I want to be sure that it doesn't need reallocation Or otherwise I can allocate memory on heap without needing std::string.
@user3234: std::string has a reserve member function that behaves just as std::vector::resize does. If you need to append another character but size() == capacity(), there's really no way around a reallocation.
4

Implementation specific - std::String usually allocates a small starting string, 16bytes is common. It's a compromise between not having to do a realloc and move for very short strings and not wasting space

Comments

0

It's an implementation detail on which you're not supposed to rely on; to see exactly how std::string grow in your implementation you can have a look at the sources of its CRT. In general it has an exponential grow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.