What is the overhead in the string structure that causes sizeof() to be 32 ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Most modern
std::stringimplementations1 save very small strings directly on the stack in a statically sizedchararray instead of using dynamic heap storage. This is known as Small (or Short) String Optimisation (SSO). It allows implementations to avoid heap allocations for small string objects and improves locality of reference.Furthermore, there will be a
std::size_tmember to save the strings size and a pointer to the actualcharstorage.How this is specifically implemented differs but something along the following lines works:
On typical architectures where
sizeof (void*)= 8, this gives us a total size of 32 bytes.1 The “big three” (GCC’s libstdc++ since version 5, Clang’s libc++ and MSVC’s implementation) all do it. Others may too.