Project 12.04 Section 12 ⚡ Embedded Relevance: Medium Node Pointers Memory Layout Heap

12.04 Manual Node Linking, Memory Layout, and Pointer Overhead

Executive Summary: Exploring explicit node pointer linking and traversing heap-allocated structures.

💻 1. Annotated Source Code

#ifndef NODE_H
#define NODE_H

class Node {
	public:
		Node(int data, Node* next) : data(data), next(next) {
		}
		
		void setData(int data) {
			this->data = data;
		}

		void setNext(Node* next) {
			this->next = next;
		}

		int getData() const {
			return data;
		}

		Node* getNext() const {
			return next;
		}

	private:
		int data;
		Node* next;
};

#endif 
#include <iostream>
#include "Node.h"

using namespace std;

Node* createChain();
void printChain(Node* const head);
void deleteChain(Node*& head);

int main() {

	Node* theHead = createChain();

	printChain(theHead);
	deleteChain(theHead);
}


Node* createChain() {
	Node* head = nullptr;

	for (int i = 0; i < 25; i++) {
		head = new Node(i, head);
	}

	return head;
}

void printChain(Node* const head) {
	Node* walker = head;  
	int count = 0;

	while (walker != nullptr) {
		cout << walker->getData() << endl;
		walker = walker->getNext();
		count++;
	}

	cout << "Number of elements: " << count << endl;

}

void deleteChain(Node*& head) {
	Node* nodeToDelete;

	while (head != nullptr) {
		nodeToDelete = head;
		head = head->getNext();
		delete nodeToDelete;
	}
}

📐 2. Architecture & UML Class Model

📐 LinkedChain Node Pointer-Chaining Architecture
+ Public - Private # Protected
<<struct>> Node<T> Chain Node
+item : T
+next : Node<T>* (Heap pointer)
+Node(anItem: const T&)
+Node(anItem: const T&, nextNodePtr: Node<T>*)
<<template class>> LinkedChain<T> Pointer Chain
-headPtr : Node<T>*
-itemCount : size_t
+LinkedChain()
+~LinkedChain()
+add(newEntry: const T&) : bool
+remove(anEntry: const T&) : bool
+clear() : void
+contains(anEntry: const T&) : bool const
+getLength() : size_t const
🔗 Architectural Relationships & Hierarchy
LinkedChain<T> ◆── chains nodes ◆── Node<T>

📚 3. Core C++ Concepts Deep-Dive

Node Anatomy & Pointer Chaining

Each node encapsulates a data payload and a pointer to the next node.

🔗 Singly Linked Node Pointer Chain

Data (4B) next* Data (4B) next* Data (4B) null

⚡ 4. Embedded Systems & Hardware Reality

Pointer Memory Tax

On 64-bit systems, storing a 4-byte int with an 8-byte next pointer incurs 200% memory overhead plus allocator metadata.

💡 5. Production-Ready Embedded Refactoring

💡 Production-Ready Refactor
// Intrusive Node pattern to save memory
struct IntrusiveNode { IntrusiveNode* next = nullptr; };

📝 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 a major downside of singly linked chains on memory-constrained systems?
A Pointer overhead per node and heap fragmentation from individual node allocations.
B They cannot store characters.
C Traversal is O(1).
D They require an FPU.
Detailed Explanation: Every node incurs pointer storage overhead and separate heap allocation metadata.