Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is sizeof(string) == 32?

Tags:

c++

string

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

like image 940
agam Avatar asked Sep 22 '10 15:09

agam


People also ask

What is the sizeof string?

sizeof(string) tells you the size of the pointer. 4 bytes (You're on a 32-bit machine.) You've allocated no memory yet to hold text.

Can we use sizeof for strings?

String Length in C Using the sizeof() OperatorWe can also find the length of a string using the sizeof() Operator in C. The sizeof is a unary operator which returns the size of an entity (variable or value). The value is written with the sizeof operator which returns its size (in bytes).

How big is a string in C++?

When we discuss the length of a string, we're usually referring to the number of that string's characters. In C++, string length really represents the number of bytes used to encode the given string. Since one byte in C++ usually maps to one character, this metric mostly means “number of characters,” too.

What is the size of a string variable?

A variable-length string can contain up to approximately 2 billion (2^31) characters. A fixed-length string can contain 1 to approximately 64 K (2^16) characters.


2 Answers

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.

like image 130
Konrad Rudolph Avatar answered Sep 22 '22 21:09

Konrad Rudolph


std::string typically contains a buffer for the "small string optimization" --- if the string is less than the buffer size then no heap allocation is required.

like image 44
Anthony Williams Avatar answered Sep 22 '22 21:09

Anthony Williams