Home C++ Lists Functions max_size() C++ std::list max_size() Function 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.
std::size_t limit = l.max_size(); // theoretical limit
bool fits = l.size() + 10 <= l.max_size(); Call What 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 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_typeThe theoretical maximum number of elements. The exact value depends on the compiler, the system, and the element type.
C++ Examples 1. A huge number 2. Check the room left 3. Same for all lists 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.
#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;
} bigger than a million: true
bigger than size: true How It Works On a modern computer max_size() is far beyond a million, because it is limited by the address space. The empty list has size 0, so the limit is always bigger than the current size. 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.
#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;
} can add 100: true
can add max_size: false How It Works max_size() - size() is the number of extra elements that fit, in theory.For 100 extra elements the answer is true, because the list holds only three elements. 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.
#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 List a is empty and list b holds one thousand sevens. max_size() depends on the element type and the allocator, not on how many elements a list holds now.Both lists have type std::list<int>, so the limit is the same, and the result is true. Frequently Asked Questions What does std::list::max_size() return? The largest number of elements that the list could hold in theory on your system. It is an upper limit, not the current size.
Can I really store max_size() elements? No. The limit is a theoretical value. A real program runs out of memory long before that.
What is the difference between size() and max_size()? size() is the count of elements stored now. max_size() is the largest count the library would allow.
Is max_size() the same on every computer? 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.
Why would I ever call max_size()? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion