Project 4.02 Section 4 ⚡ Embedded Relevance: Core Array Initialization Value Init {} Garbage RAM sizeof BSS vs Stack

4.02 Array Size Calculation, Garbage Stack Data & Value-Initialization ({})

Executive Summary: Analyzing array initialization syntax, calculating element counts via sizeof, and the dangers of uninitialized stack variables containing random SRAM power-on garbage.

💻 1. Annotated Source Code

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

int main() {

	array<int, 5> myIntArray{1, 2};

	myIntArray[0] = 2;
	myIntArray[1] = 5;
	myIntArray[2] = 10;
	myIntArray[3] = 1;
	myIntArray[4] = 17;


	for (int a : myIntArray) {
		cout << a << endl;
	}

	cout << "Size of the array: " << myIntArray.size() << endl;

	return 0;
}

📐 2. Architecture & UML Class Model

📐 Array Boundary Verification & Assertion Test Harness
+ Public - Private # Protected
<<compilation-unit>> ArrayTestHarness Test Harness
-testBuffer[10] : int32_t
+testIndexRead(idx: size_t) : bool
+testIndexWrite(idx: size_t, val: int) : bool

📚 3. Core C++ Concepts Deep-Dive

1. Uninitialized Stack Arrays vs Value-Initialization

Declaring int arr[5]; leaves memory uninitialized. Reading these elements reads whatever residual charges remained in SRAM silicon (garbage values). Using int arr[5]{}; or int arr[5] = {0}; value-initializes all elements to zero.

2. Element Count Idiom

In C++11 and earlier, array length was computed via sizeof(arr) / sizeof(arr[0]). In C++17+, std::size(arr) provides a type-safe alternative.

⚡ 4. Embedded Systems & Hardware Reality

1. Power-On SRAM Residual State

When a microcontroller powers on, SRAM bit cells power up in unpredictable states determined by silicon transistor mismatch. Uninitialized stack variables can cause intermittent, hardware-dependent bugs that disappear during debugging.

2. Zero-Cost .bss Initialization

Global/static uninitialized arrays are placed in the .bss section, which the C runtime startup code (Reset_Handler) clears to zero before main() is invoked.

💡 5. Production-Ready Embedded Refactoring

Modern C++ guarantees zero-initialization with clean syntax:

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

// Guaranteed zero-initialized on the stack (zero garbage RAM)
std::array<uint32_t, 16> telemetry_buffer{};

// Compile-time verified size
constexpr size_t BUFFER_LEN = std::size(telemetry_buffer);

📝 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. What is stored in an array declared as 'int buffer[10];' on the stack in C++?
A Indeterminate (garbage) values from residual SRAM charges
B All zeros
C All negative ones
D Null pointers
Detailed Explanation: Local stack variables without an explicit initializer are default-initialized, which for fundamental types means their memory retains whatever garbage was previously in that stack location.
Q2. How does 'int buffer[10]{};' differ from 'int buffer[10];'?
A The empty braces {} guarantee that all 10 elements are zero-initialized
B It allocates memory on the heap
C It makes the array read-only
D It converts the array into a pointer
Detailed Explanation: Value-initialization with {} initializes fundamental numeric types to zero.
Q3. Which section of microcontroller memory holds global uninitialized variables and is cleared to zero during startup?
A .bss section
B .text section
C .rodata section
D .heap section
Detailed Explanation: The .bss section contains uninitialized global and static variables. The startup assembly routine (Reset_Handler) zeroes this region before jumping to main().
Q4. What is the benefit of std::size(arr) over sizeof(arr)/sizeof(arr[0]) in C++17?
A std::size will fail to compile if passed a decayed pointer, preventing accidental size bugs
B std::size calculates size in megabytes
C std::size executes faster at runtime
D std::size works with void pointers
Detailed Explanation: std::size() expects a container or fixed array reference. Passing a decayed pointer causes a compilation error rather than silently returning a wrong calculation.