devnotes.

Arrays and std::array

C-style arrays work identically in C++, with all the same limitations (fixed size, no bounds checking, decay to a pointer when passed to functions). C++11 added std::array as a safer, modern alternative for fixed-size collections.

int cArray[5] = {1, 2, 3, 4, 5};       // C-style array — still works, still has C's gotchas

#include <array>
std::array<int, 5> arr = {1, 2, 3, 4, 5}; // std::array — same fixed size, more features

std::cout << arr.size() << "\n";       // 5 — knows its own size, unlike a C array
std::cout << arr.at(2) << "\n";        // 3 — .at() bounds-checks, throws if out of range
arr[10];                                 // still allowed, still undefined behavior, like C

std::array<T, N> wraps a fixed-size C array with the same performance characteristics (no heap allocation, no extra overhead) but adds genuinely useful member functions: .size() returns the element count without the sizeof(arr)/sizeof(arr[0]) trick C requires, and .at(i) performs bounds checking, throwing a std::out_of_range exception on an invalid index instead of silently corrupting memory — operator[] (arr[i]) remains unchecked for maximum speed, matching plain array behavior, so the choice between [] and .at() is a deliberate safety-versus-speed trade-off.

For collections whose size isn't known ahead of time or needs to grow, C++ provides std::vector — covered in the STL Containers chapter — which is almost always the better default over a raw, fixed-size C array in modern C++ code.

Arrays and std::array in C++ | DevNotes