C++ std::deque max_size() Function

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

Definition

In C++, std::deque::max_size() returns the largest number of elements that a std::deque could theoretically hold. The limit comes from the element type and the library, not from your free memory.

The number is usually huge. Real programs run out of memory long before they reach it. Include <deque> to use it.

This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.

Syntax

Call max_size with empty brackets. It takes no arguments.

C++
std::size_t limit = d.max_size();
CallWhat it does
d.max_size()Returns the largest possible element count for this deque type.

size() tells you how many elements the deque holds now. max_size() is only an upper limit.

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_typeThe theoretical maximum number of elements. The exact value depends on the compiler, the system, and the element type.

C++ Examples

Example 1 — Basic max_size()

The exact number differs per computer, so the example checks simple facts instead.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d;

    std::cout << std::boolalpha;
    std::cout << "max_size > 1000000: " << (d.max_size() > 1000000) << '\n';
    std::cout << "max_size > size:    " << (d.max_size() > d.size()) << '\n';
    return 0;
}

How It Works

  1. max_size() is a very large number on normal computers, so it is bigger than one million.
  2. It is also bigger than size(), which is 0 for a new deque.
  3. The code prints true and false instead of the raw number, so the output is the same on every system.

Example 2 — Check There Is Room to Add More

Compare the free count (max_size() - size()) with the number you want to add.

C++
#include <deque>
#include <iostream>

bool canAdd(const std::deque<int>& d, std::size_t extra) {
    return d.max_size() - d.size() >= extra;
}

int main() {
    std::deque<int> d = {1, 2, 3};
    std::size_t huge = d.max_size();

    std::cout << std::boolalpha;
    std::cout << "can add 5:    " << canAdd(d, 5) << '\n';
    std::cout << "can add huge: " << canAdd(d, huge) << '\n';
    return 0;
}

How It Works

  1. max_size() - size() is the number of elements that could still be added in theory.
  2. Adding 5 more elements is fine, so canAdd(d, 5) returns true.
  3. The deque already has 3 elements, so asking for max_size() more elements would go over the limit, and the result is false.

Example 3 — Element Size Changes the Limit

Small elements allow more of them than big elements.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<char> small;
    std::deque<int> big;

    std::cout << std::boolalpha;
    std::cout << "deque<char> max_size >= deque<int> max_size: "
              << (small.max_size() >= big.max_size()) << '\n';
    return 0;
}

How It Works

  1. A char uses 1 byte, and an int usually uses 4 bytes.
  2. The limit depends on how many bytes one element needs, so a deque of char can hold at least as many elements as a deque of int.
  3. The comparison prints true. The exact numbers are left out because they change between compilers.

Frequently Asked Questions

It returns the theoretical largest number of elements that the deque can hold, based on the element type and the library.
No. It is only a theoretical upper limit. The deque will usually fail with std::bad_alloc because memory runs out much earlier.
size() is the number of elements in the deque right now. max_size() is the most elements it could ever have.
The value depends on the compiler, the library, 32-bit or 64-bit mode, and the element type. Never depend on one exact number.
No. A deque has size() and max_size(), but no capacity() and no reserve(). It grows block by block.

Next: pop_back()

Remove the last element with std::deque.

pop_back() tutorial →

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.

9 people found this page helpful