C++ std::list max_size() Function

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

Definition

In C++, std::list::max_size() returns the largest number of elements that a std::list could hold in theory. It is a limit set by the library and your computer, such as the size of a pointer.

The number is huge, so you almost never reach it. Before that, the program usually runs out of memory. Include <list> to use it.

This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.

Syntax

Call max_size with empty brackets. It returns an unsigned number.

C++
std::size_t limit = l.max_size();   // theoretical limit
bool fits = l.size() + 10 <= l.max_size();
CallWhat it does
l.max_size()Returns the theoretical maximum number of elements, as a size_type.

max_size() is not a promise that you can store that many elements. Memory may run out much sooner.

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 — max_size() Is Very Large

We do not print the exact value, because it depends on your system. We only check that it is big.

C++
#include <iostream>
#include <list>

int main() {
    std::list<int> l;

    std::cout << std::boolalpha;
    std::cout << "bigger than a million: " << (l.max_size() > 1000000) << '\n';
    std::cout << "bigger than size: " << (l.max_size() > l.size()) << '\n';
    return 0;
}

How It Works

  1. On a modern computer max_size() is far beyond a million, because it is limited by the address space.
  2. The empty list has size 0, so the limit is always bigger than the current size.
  3. Both checks print true on every normal system. We avoid printing the raw number because it changes from compiler to compiler.

Example 2 — max_size() to Check the Room Left

Write a small test that asks whether a list can still take a number of new elements.

C++
#include <iostream>
#include <list>

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

int main() {
    std::list<int> l = {1, 2, 3};

    std::cout << std::boolalpha;
    std::cout << "can add 100: " << canAdd(l, 100) << '\n';
    std::cout << "can add max_size: " << canAdd(l, l.max_size()) << '\n';
    return 0;
}

How It Works

  1. max_size() - size() is the number of extra elements that fit, in theory.
  2. For 100 extra elements the answer is true, because the list holds only three elements.
  3. Adding max_size() more elements is impossible, because three are already there, so the answer is false.

Example 3 — max_size() Is the Same for Equal Types

The limit comes from the list type and its allocator, not from the contents.

C++
#include <iostream>
#include <list>

int main() {
    std::list<int> a;
    std::list<int> b(1000, 7);

    std::cout << std::boolalpha;
    std::cout << "same limit: " << (a.max_size() == b.max_size()) << '\n';
    return 0;
}

How It Works

  1. List a is empty and list b holds one thousand sevens.
  2. max_size() depends on the element type and the allocator, not on how many elements a list holds now.
  3. Both lists have type std::list<int>, so the limit is the same, and the result is true.

Frequently Asked Questions

The largest number of elements that the list could hold in theory on your system. It is an upper limit, not the current size.
No. The limit is a theoretical value. A real program runs out of memory long before that.
size() is the count of elements stored now. max_size() is the largest count the library would allow.
No. It depends on the compiler, the library, the system (32-bit or 64-bit), and the element type. Do not rely on a specific number.
Mostly to check a very large request before you make it, for example before you resize() to a number you read from a file or from a user.

Next: merge()

Merge another sorted list into this sorted list with std::list.

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

8 people found this page helpful