String in C++
String in C++
Created on Oct 28, 2025, Last Updated on Nov 03, 2025, By a Developer
Dynamic Memory Allocation
std::string is similar to std::vector<char> from the aspect of memory management. It allocates contiguous space on heap for characters, grows the space geometrically when number of char goes beyond space allocated, and copy contents across.
Small String Optimization
Most of C++ compilers implement Small String Optimization (SSO). It refers to putting the entire string on stack when the size of the string is small, usually smaller than 16 characters.
C String
In C, string is managed in a non-trivial way. String is a char[] with a \0 at the end to signal the termination of the string.
To support the interoperability with c string, std::string also follow the convention.
int main() {
const char* cstr = "hello";
std::string s(cstr);
s.c_str(); // Getting C string, type as char*.
std::string s2 = cstr; // implict conversion.
}