As far as I know, in C ++ there is an implementation-dependent optimization stringthat allows you stringnot to allocate any additional heap memory to store your characters, but rather to store the characters in the object itself string, therefore, if a line sallocates additional memory on the heap, the total consumed its memory is sizeof(string) + s.capacity(), however, if it does not allocate any additional memory on the heap, i.e. stores its characters in the object string, then the total memory consumption is sizeof(string).
Is there a way to determine this amount - the total memory consumed by the string? The problem is that I don’t see a way to find out if a string object allocates memory in a heap or not, so I don’t know which formula to use for a specific one string.
EDIT: A hack injecting something in the STL namespace to find out implementation-specific details (the threshold at which stringadditional memory starts to be allocated) will be fine if there is no other solution.
source
share