Project 5.11 Section 5 ⚡ Embedded Relevance: Core std::array Zero-Cost Abstraction Iterators Const Reference Clean Code

5.11 std::array Member Encapsulation, Iterators & Zero-Overhead C++ Idioms

Executive Summary: Using std::array as an object container. We demonstrate how std::array provides STL iterator compatibility (begin/end) and value-type semantics while generating assembly identical to raw C arrays.

💻 1. Annotated Source Code

#include <iostream>
#include <array>
using namespace std;

int productArray(array<int, 6> myArray);

int main() {
	array<int, 6> numbers{ 1, 2, 3, 4, 5, 6 };

	int result = productArray(numbers);

	cout << "The product of the array elements is: " << result << endl;

	return 0;
}

int productArray(array<int, 6> myArray) {
	int product = 1;

	for (int num : myArray) {
		product *= num;
	}

	return product;
}

📐 2. Architecture & UML Class Model

📐 Array Container Object & Encapsulated Multiplication
+ Public - Private # Protected
<<struct>> ProductArrayObject Array Wrapper
+values[5] : int32_t
+length : size_t = 5
+getProduct() : int64_t const

📚 3. Core C++ Concepts Deep-Dive

1. std::array as a First-Class Object

Unlike raw C arrays, std::array behaves as a first-class C++ object: it can be assigned (=), passed by value/reference, returned from functions, and queried for size (.size()).

⚡ 4. Embedded Systems & Hardware Reality

1. Zero-Cost Abstraction Verification

Disassembling std::array member access in GCC/Clang reveals that arr[i] compiles to the exact same single-instruction memory load (LDR) as a raw C array, incurring zero performance or memory penalty.

💡 5. Production-Ready Embedded Refactoring

Functional array multiplication using standard algorithms:

💡 Production-Ready Refactor
#include <cstdint>
#include <array>
#include <numeric>

template <size_t N>
uint32_t computeArrayProduct(const std::array<uint32_t, N>& arr) noexcept {
    return std::accumulate(arr.begin(), arr.end(), 1UL, std::multiplies<uint32_t>());
}

📝 Knowledge Verification Quiz

Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.

Q1. Why is std::array considered a 'zero-cost abstraction' in C++?
A It wraps a raw C array with modern container interfaces without adding any memory overhead or runtime instruction penalties
B It is free to download from GitHub
C It uses 0 bytes of Flash memory
D It requires no CPU power
Detailed Explanation: std::array contains only the underlying array; all member functions are inline and compile to identical assembly as raw C arrays.
Q2. Can std::array be returned by value from a function without dynamic memory allocation?
A Yes, std::array is a value type that resides on the stack and is returned via Return Value Optimization (RVO)
B No, it requires malloc
C Only in C++23
D Only if size is 1
Detailed Explanation: std::array is a standard value struct stored on the stack; modern compilers return it with zero heap allocation using RVO.
Q3. What happens if you assign one std::array to another of the same type and size (arr1 = arr2)?
A A member-wise copy of all elements from arr2 to arr1 is performed
B Only the pointer address is copied
C A compiler error occurs
D Memory is allocated on the heap
Detailed Explanation: std::array defines value copy assignment, copying all elements directly.
Q4. Which method on std::array returns a raw pointer to the underlying contiguous C array?
A .data()
B .raw()
C .get_ptr()
D .pointer()
Detailed Explanation: arr.data() returns a direct pointer (T*) to the underlying contiguous buffer for C API compatibility.