C++ std::string max_size() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

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();
CallWhat 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

ParameterDescription
Nonemax_size() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
size_typeMaximum number of characters the string is allowed to hold.

C++ Examples

Example 1 — Show max_size()

Print the theoretical maximum.

C++
#include <iostream>
#include <string>

int main() {
    std::string s;
    std::cout << "max_size: " << s.max_size() << '\n';
    return 0;
}

How It Works

  1. A default string still knows the type limit.
  2. max_size() returns a very large number on a 64-bit library (exact value varies).
  3. Your machine cannot actually allocate that many characters.

Example 2 — size Versus max_size

Show that normal sizes are tiny next to max_size.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "hello";
    std::cout << "size: " << s.size() << '\n';
    std::cout << "max:  " << s.max_size() << '\n';
    std::cout << std::boolalpha
              << "size < max: " << (s.size() < s.max_size()) << '\n';
    return 0;
}

How It Works

  1. The live string only uses five characters.
  2. max_size() is the type ceiling, not the current length.
  3. Everyday sizes are always far below that ceiling.

Example 3 — Reserve Far Below max_size

Reserve a practical amount instead of the maximum.

C++
#include <iostream>
#include <string>

int main() {
    std::string s;
    s.reserve(64);
    std::cout << "capacity: " << s.capacity() << '\n';
    std::cout << "max_size: " << s.max_size() << '\n';
    return 0;
}

How It Works

  1. reserve(64) asks for a small, useful buffer.
  2. Capacity becomes at least 64.
  3. max_size() stays huge and is not used as the reserve argument.

Frequently Asked Questions

It returns the largest character count this string type could theoretically hold.
Almost never. Memory limits stop you much earlier.
No. Reserve a realistic size for your data.
No. It is a property of the string type and allocator, not of the current buffer.
No. It depends on the standard library and whether you build 32-bit or 64-bit.

Next: pop_back()

Keep going through the string functions. Remove the last character. Undefined on empty strings.

pop_back() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful