Here is a short program to help you explore both kinds of memory usage of std::string
: stack and heap.
#include <string>
#include <new>
#include <cstdio>
#include <cstdlib>
std::size_t allocated = 0;
void* operator new (size_t sz)
{
void* p = std::malloc(sz);
allocated += sz;
return p;
}
void operator delete(void* p) noexcept
{
return std::free(p);
}
int
main()
{
allocated = 0;
std::string s("hi");
std::printf("stack space = %zu, heap space = %zu, capacity = %zu\n",
sizeof(s), allocated, s.capacity());
}
Using http://melpon.org/wandbox/ it is easy to get output for different compiler/lib combinations, for example:
gcc 4.9.1:
stack space = 8, heap space = 27, capacity = 2
gcc 5.0.0:
stack space = 32, heap space = 0, capacity = 15
clang/libc++:
stack space = 24, heap space = 0, capacity = 22
VS-2015:
stack space = 32, heap space = 0, capacity = 15
(the last line is from http://webcompiler.cloudapp.net)
The above output also shows capacity
, which is a measure of how many char
s the string can hold before it has to allocate a new, larger buffer from the heap. For the gcc-5.0, libc++, and VS-2015 implementations, this is a measure of the short string buffer. That is, the size buffer allocated on the stack to hold short strings, thus avoiding the more expensive heap allocation.
It appears that the libc++ implementation has the smallest (stack usage) of the short-string implementations, and yet contains the largest of the short string buffers. And if you count total memory usage (stack + heap), libc++ has the smallest total memory usage for this 2-character string among all 4 of these implementations.
It should be noted that all of these measurements were taken on 64 bit platforms. On 32 bit, the libc++ stack usage will go down to 12, and the small string buffer goes down to 10. I don't know the behavior of the other implementations on 32 bit platforms, but you can use the above code to find out.
std::string
implementation is not C++11 compliant and uses a copy on write implementation which gives you the size savings. Rerun your libstdc++ test usingvstring
instead and compare the results. – Praetorian Dec 24 '14 at 3:35sizeof(std::string)
does not represent all the memory occupied by the string, but only the memory occupied by the string class (say, on the stack, for a stack-allocated string), and not any data structures it points to. – EyasSH Dec 24 '14 at 3:38__gnu_cxx::__vstring
with libstdc++. And what EyasSH means is that the size of thestd::string
object is not affected by the length of the string it manages. – Praetorian Dec 24 '14 at 3:48