In C++, std::vector::max_size() returns the largest number of elements that a std::vector 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 <vector> to use it.
This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.
Syntax
Call max_size with empty brackets. It takes no arguments.
C++
std::size_t limit = v.max_size();
Call
What it does
v.max_size()
Returns the largest possible element count for this vector type.
size() tells you how many elements the vector holds now. max_size() is only an upper limit.
Library declarations
C++
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_type
The 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.