This is implementation-defined, but as an example, the std::basic_string
implementation on Visual C++ 2015 Update 3 will use an internal array if the length of the string is less than 16 bytes. Here's a heavily edited portion of _String_val
from <xstring>
:
template<class _Val_types>
class _String_val
{
public:
typedef typename _Val_types::value_type value_type;
enum
{ // length of internal buffer, [1, 16]
_BUF_SIZE = 16 / sizeof (value_type) < 1 ? 1
: 16 / sizeof (value_type)
};
value_type *_Myptr()
{ // determine current pointer to buffer for mutable string
return (this->_BUF_SIZE <= _Myres
? _Bx._Ptr
: _Bx._Buf);
}
union _Bxty
{ // storage for small buffer or pointer to larger one
value_type _Buf[_BUF_SIZE];
pointer _Ptr;
} _Bx;
};
If you look at _Myptr()
, you'll notice _Buf
is used when the length is less than _BUF_SIZE
and _Ptr
is used otherwise.
Needless to say, you shouldn't rely on this being true for anything else than this particular implementation of the standard library. It is an internal detail that may change at any time.