Question: Consider the following test program:

```
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::cout << sizeof(std::string("hi")) << " ";
    std::string a[10];
    std::cout << sizeof(a) << " ";
    std::vector<std::string> v(10);
    std::cout << sizeof(v) + sizeof(std::string) * v.capacity() << "\n";
}

```

Output for `libstdc++` and `libc++` respectively are:

```
8 80 104
24 240 264

```

As you can see, `libc++` takes 3 times as much memory for a simple program. How does the implementation differ that causes this memory disparity? Do I need to be concerned and how do I workaround it?

Answer: 
You should not be concerned, standard library implementors know what they are doing.

Using the latest code from the GCC subversion trunk libstdc++ gives these numbers:

```
32 320 344

```

This is because as of a few weeks ago I switched the default `std::string` implementation to use the small-string optimisation (with space for 15 chars) instead of the copy-on-write implementation that you tested with.
I haven't checked the actual implementations in source code, but I remember checking this when I was working on my C++ string library. A 24 byte string implementation is typical. If the length of the string is smaller than or equal to 16 bytes, instead of malloc'ing from the heap, it copies the string into the internal buffer of size 16 bytes. Otherwise, it mallocs and stores the memory address etc. This minor buffering actually helps in terms of running time performance.

For some compilers, there's an option to turn the internal buffer off.