C++ std::string reserve() Function

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

Definition

In C++, std::string::reserve() asks the string to make sure its buffer can hold at least n characters without another allocation.

It does not add characters and does not change 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

Pass the capacity you want to prepare for.

C++
s.reserve(256);
s += "lots of text...";
CallWhat it does
s.reserve(n)Ensure capacity is at least n. May allocate now.
s.reserve()C++20: non-binding shrink request similar in spirit to shrink_to_fit (implementation-defined). Prefer shrink_to_fit() for intent.

If n is less than or equal to the current capacity, most libraries do nothing. reserve never reduces size().

Library declarations
C++
void reserve(size_type new_cap = 0);

Parameters

ParameterDescription
new_capDesired minimum capacity. Optional; meaning of the zero-argument form is a non-binding shrink request since C++20.

Return Value

Return ValueDescription
voidNo return value. Check capacity() afterward if you need confirmation.

C++ Examples

Example 1 — Basic reserve()

Reserve space, then append without surprise growth from tiny starts.

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

int main() {
    std::string s;
    s.reserve(50);
    std::cout << "cap: " << s.capacity() << '\n';
    s.append("hello");
    std::cout << "size: " << s.size() << '\n';
    std::cout << "cap: " << s.capacity() << '\n';
    return 0;
}

How It Works

  1. reserve(50) grows capacity to at least 50.
  2. Appending five characters only changes size.
  3. Capacity stays large enough, so no second allocation is needed for that append.

Example 2 — Reserve Before a Loop

Build a string from many pieces after one reserve.

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

int main() {
    std::string out;
    out.reserve(20);
    for (int i = 0; i < 5; ++i) {
        out += "ab";
    }
    std::cout << out << '\n';
    std::cout << "size: " << out.size() << '\n';
    return 0;
}

How It Works

  1. One reserve covers the ten characters we plan to add.
  2. The loop appends "ab" five times.
  3. Result is ten characters with fewer reallocations than growing from zero repeatedly.

Example 3 — Reserve Does Not Erase

Show that reserve leaves the text alone.

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

int main() {
    std::string s = "C++";
    s.reserve(40);
    std::cout << s << '\n';
    std::cout << "size: " << s.size() << '\n';
    return 0;
}

How It Works

  1. The string already holds "C++".
  2. reserve(40) only adjusts capacity planning.
  3. The visible text and size stay 3.

Frequently Asked Questions

It requests that capacity be at least n so later growth can avoid extra allocations.
No. The character count stays the same.
Before a loop that appends many times, or when you know an approximate final length.
Yes, if allocation fails it can throw std::bad_alloc.
Use shrink_to_fit() as a non-binding request. There is no guarantee the library frees memory.

Next: resize()

Keep going through the string functions. Change the number of characters; pad or truncate.

resize() 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.

10 people found this page helpful