It depends on the implementation, but the general idea is that the string class will contain a pointer to the memory area in which the actual contents of the string are stored. In two general implementations, 3 pointers are stored (the beginning of the selected area and data, the end of the data, the end of the selected area) or a pointer (the beginning of the selected area and data) and two integers (the number of characters per line and the number of bytes allocated).
When new data is added to the line, if it corresponds to the selected area, it will simply be written, and accordingly the pointer to the size / end of the pointer will be updated. If the data does not correspond to the region, a new buffer will be created and the data will be copied.
Also note that in many implementations there are optimizations for small strings, where the string class contains a small buffer. If the contents of the string fit into the buffer, then the memory is not allocated dynamically and only the local buffer is used.
David Rodríguez - dribeas
source share