0Pricing
C++ Academy · Lesson

Fixed-Size std::array

A safe C-array replacement.

Fixed-Size std::array is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

A Safe C-Array

std::array<T, N> is a fixed-size array that knows its own size and behaves like a proper container. Include <array>.

#include <iostream>
#include <array>
int main() {
    std::array<int, 3> a = {1, 2, 3};
    std::cout << a[0] << " " << a.size() << "\n";
}

Knows Its Size

Unlike a raw array that decays to a pointer, std::array always reports its length via size().

#include <iostream>
#include <array>
int main() {
    std::array<double, 4> a{};
    std::cout << a.size() << "\n"; // 4
}

Range-Based For

Because it is a proper container, you can iterate with a range-based for loop.

#include <iostream>
#include <array>
int main() {
    std::array<int, 3> a = {10, 20, 30};
    for (int x : a) std::cout << x << " ";
    std::cout << "\n";
}

Bounds-Checked at()

at(i) throws std::out_of_range on a bad index, while operator[] does not check, choose based on safety needs.

#include <iostream>
#include <array>
int main() {
    std::array<int, 2> a = {1, 2};
    try { a.at(5); }
    catch (const std::out_of_range&) { std::cout << "out of range\n"; }
}

front, back, and data

Convenience accessors: front() and back() return the first and last elements; data() gives a raw pointer for C APIs.

#include <iostream>
#include <array>
int main() {
    std::array<int, 3> a = {7, 8, 9};
    std::cout << a.front() << " " << a.back() << "\n";
}

Copyable by Value

std::array copies cleanly with =, copying every element, unlike raw arrays which decay to pointers.

#include <iostream>
#include <array>
int main() {
    std::array<int, 3> a = {1, 2, 3};
    std::array<int, 3> b = a; // full copy
    b[0] = 99;
    std::cout << a[0] << " " << b[0] << "\n"; // 1 99
}

Works with Algorithms

Standard algorithms accept std::array iterators, here we sort it.

#include <iostream>
#include <array>
#include <algorithm>
int main() {
    std::array<int, 4> a = {4, 1, 3, 2};
    std::sort(a.begin(), a.end());
    for (int x : a) std::cout << x << " ";
    std::cout << "\n";
}

Fill and Compare

fill(value) sets every element; std::array also supports element-wise == comparison.

#include <iostream>
#include <array>
int main() {
    std::array<int, 3> a{};
    a.fill(5);
    std::array<int, 3> b = {5, 5, 5};
    std::cout << (a == b) << "\n"; // 1
}

Size Is Part of the Type

The length N is baked into the type, so a function taking std::array<int,3> will not accept a size-4 array. This is where std::span helps later.

Stack Allocation

std::array stores its elements inline (typically on the stack), with no heap allocation, making it fast and predictable for fixed-size data.

Array vs vector

Use std::array when the size is known at compile time and fixed. Use std::vector when the size can change at runtime.

Quick Check

Check your understanding of std::array.

Recap

You learned std::array:

  • Fixed-size, size-aware container, no heap allocation.
  • at() bounds-checks; [] does not.
  • Copyable, comparable, and works with algorithms.
  • Size N is part of the type.

Frequently asked questions

Is the “Fixed-Size std::array” lesson free?

Yes — the full text of “Fixed-Size std::array” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Fixed-Size std::array”?

A safe C-array replacement. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Fixed-Size std::array” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Fixed-Size std::array
  2. std::span Basics
  3. Passing Spans to Functions
  4. Bounds and Subspans
← Back to C++ Academy