C++ std::array max_size() Function

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

Definition

In C++, std::array::max_size() returns the largest number of elements the array can ever hold. For a std::array<T, N> that number is simply N.

That makes it the same as size() for arrays. The function exists because all standard containers have it. Include <array> to use it.

This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.

Syntax

Call max_size with empty brackets. It takes no arguments.

C++
std::size_t limit = nums.max_size();
CallWhat it does
nums.max_size()Returns the most elements this array can hold. For std::array<int, 5> that is 5.
nums.max_size() == nums.size()Always true for a std::array.

For a std::vector, max_size() is a huge number. For std::array it is just N.

Library declarations
C++
constexpr size_type max_size() const noexcept;

Parameters

ParameterDescription
Nonemax_size() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
SizeThe value N from std::array<T, N>, of type size_type.

C++ Examples

Example 1 — Basic max_size()

Print size() and max_size() for the same array.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 5> nums = {1, 2, 3, 4, 5};

    std::cout << "size: " << nums.size() << '\n';
    std::cout << "max_size: " << nums.max_size() << '\n';
    return 0;
}

How It Works

  1. The array type says it holds exactly 5 ints.
  2. size() reports the current number of elements, which is 5.
  3. max_size() reports the limit, which is also 5 because an array can never grow.

Example 2 — Different Types and Sizes

The answer depends only on N, not on the element type.

C++
#include <array>
#include <iostream>

int main() {
    std::array<char, 10> letters;
    std::array<double, 2> prices;

    std::cout << "char array max_size: " << letters.max_size() << '\n';
    std::cout << "double array max_size: " << prices.max_size() << '\n';
    return 0;
}

How It Works

  1. letters is declared with N = 10, so its limit is 10.
  2. prices is declared with N = 2, so its limit is 2.
  3. A char uses less memory than a double, but that does not change the count.

Example 3 — Check Against the Limit

Decide whether a number of items can fit before you try to store them.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 6> slots{};
    std::size_t wanted = 10;

    if (wanted > slots.max_size()) {
        std::cout << "Cannot store " << wanted << " items; the limit is "
                  << slots.max_size() << '\n';
    } else {
        std::cout << "Everything fits\n";
    }
    return 0;
}

How It Works

  1. slots has room for 6 ints, and that can never change.
  2. The program wants to store 10, which is more than max_size().
  3. The if branch runs and prints a clear message, so no data is written outside the array.

Frequently Asked Questions

It returns the largest number of elements the array can hold. For std::array<T, N> that is always N.
No. For a std::array both return N. They differ for containers such as std::vector, where max_size() is a huge limit and size() is the current count.
Yes. It is a constexpr function, so you can use it in places that need a constant, like static_assert or a template argument.
All standard containers share the same set of functions. That lets template code treat a std::array and a std::vector in the same way.
Use size() in normal code. It says what you mean. max_size() is mostly useful in generic code that works with many container types.

Next: rbegin()

Get a reverse iterator to the last element with std::array.

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

10 people found this page helpful