Why Is Sizeof(Std::String) Only Eight Bytes

Why is sizeof(string) == 32?

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 <typename T>
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.

Why is sizeof(str) 8 when the string has 26 chars?

On your platform, a char pointer is 8 bytes. That's the size of the pointer variable, not the size of whatever it's pointing to. If you want the length of a string, call strlen(myarr).

Why does Sizeof operator on a std::string yield unexpected result?

sizeof of a std::string instance just returns the size, in bytes, of the "internal representation" of the std::string, i.e. you can think of it like the sum of the sizeofs of each std::string's data members (there may be padding involved as well).

For example, in 32-bit debug builds with VS2015, sizeof(std::string) returns 28; in 64-bit debug builds I get 40; in 32-bit release builds I get 24, and in 64-bit release builds 32.

That's because the internal representation of std::string changes with different build options: e.g. debug builds usually contain additional mechanics to help spotting bugs, which grows the size of the representation; moreover, in 64-bit builds the pointers are larger, so again the size increases with respect to 32-bit builds, etc.

So, the number you get from sizeof invoked on a std::string instance is in general different from the number of chars that make the string's text. To get this number, you have to call std::string::size or std::string::length.

Size of string vs size of char array in c++

std::string is a class object in C++. The size of it is implementation-defined, but will generally require space for at least both a pointer to a heap-allocated string, and a size. The sizeof(std::string) you are seeing may be specific to your implementation -- but it could be partially due to a small-buffer optimization where small strings are stored directly in the string.

An array of chars is just an array of characters, so sizeof(arr) will always be the the number of characters.


As for whether you can use std::string: it depends on how constrained you are. std::string will often use heap memory for larger strings, which may be hard to justify on extremely constrained systems. However, most std::string implementations also have support for small-buffer-optimized strings, where these strings are stored directly in the std::string rather than indirectly via heap memory

If heap memory is not justifyable, std::string will not be a good solution for you.

However, if that's the case, I would recommend you look into either adopting an existing static_string implementation that encodes the size as a template argument, or at least writing your own. This would be better than using char arrays everywhere, since this becomes hard to manage long-term.

c++ sizeof( string )

It isn't clear from your example what 'string' is. If you have:

#include <string>
using namespace std;

then string is std::string, and sizeof(std::string) gives you the size of the class instance and its data members, not the length of the string. To get that, use:

string s;
cout << s.size();


Related Topics



Leave a reply



Submit