The object A std::string
will be distributed in the same way as int
or any other type should be: on the stack if it is a local variable, or it can be static
, or on the heap if new std::string
used or new X
, where X
contains string
, etc.
But, that a std::string
object can contain at least a pointer to the additional memory provided by the allocator with which the basic_string <- instance was created for std::string
typedef
, which means allocated memory. Either directly in the source memory of an std::string
object, or in a heap of a point that you can find:
- member of row size
- maybe some kind of link or link counter,
- text data stored in a string (if any)
In some std::string
implementations there is an optimization of βshort linesβ, where they pack lines of only a few characters directly into the line object itself (for memory efficiency, often using some kind of union with fields that are used for other purposes when the lines are longer) . But for other string implementations and even for optimizers with short strings when working with strings that are too long for the std :: string object, they will have to follow pointers / links to text data that are stored in memory provided by the allocator (heap).
source share