It depends if it is a short or long string:
std::string a_string = a + a;
// reassign a new string to a_string
a_string = b + b + b;
First, a+a is constructed directly as a_string due to guaranteed copy elison (c++17). No freeing is going on here.
Then, if a_string
is short enough it is allocated on stack, without additional heap allocations. This Short String Optimization (SSO) is performed by most compilers, but is not mandated by the standard.
If SSO took place, then this does not free any space in a_string but merely reuses it. The memory goes nowhere:
a_string = b + b + b;
However, if a_string is too long for SSO, then this line does free heap space allocated for the string.
It is clear to see where memory goes when the declaration of std::string
is examined:
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
> class basic_string;
The memory of the non-SSO string is allocated and freed with std::allocator
. This allocator allocates and frees memory with the new
and delete
operaors. Usually they use malloc/free behind the scenes. This is how malloc and free work.
It is easy to find out how big as SSO string can be by running
std::cout << std::string().capacity() << '\n';
For clang 8.0.0 on 64 bit Intel SSO is for strings up to 22 chars, and for gcc 8.3 it is only 15 chars.