In C++, std::string::max_size() returns the largest possible character count a string of this type could theoretically hold.
You almost never hit this limit. Real programs run out of memory long before max_size(). Include <string>.
This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.
Syntax
Call max_size with empty brackets.
C++
std::size_t m = s.max_size();
Call
What it does
s.max_size()
Returns the implementation upper bound for this string type.
The value depends on the library and address model. Treat it as a theoretical ceiling, not a target size for reserve().
Library declarations
C++
size_type max_size() const noexcept;
Parameters
Parameter
Description
None
max_size() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
size_type
Maximum number of characters the string is allowed to hold.