Why is sizeof(string) == 32?

前端 未结 6 1925
再見小時候
再見小時候 2020-11-29 05:20

What is the overhead in the string structure that causes sizeof() to be 32 ?

6条回答
  •  -上瘾入骨i
    2020-11-29 05:58

    Most modern std::string implementations1 save very small strings directly on the stack in a statically sized char array 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_t member to save the strings size and a pointer to the actual char storage.

    How this is specifically implemented differs but something along the following lines works:

    template 
    struct basic_string {
        char* begin_;
        size_t size_;
        union {
            size_t capacity_;
            char sso_buffer[16];
        };
    };
    

    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.

提交回复
热议问题