2 ms·
I think most(?) C++ implementations of std::string use something called a short-string optimization, where the string type is something like struct string {
by jabl 4y ago
I think most(?) C++ implementations of std::string use something called a short-string optimization, where the string type is something like
struct string {
size_t len;
union {
char* ptr_to_malloced_string;
char short_string[sizeof(char*)];
}
}
So if the length of the string is shorter than the size of a pointer, the string is stored inline in the struct, and if the length is longer it's a separate allocated object.