6.03 Managing Multiple Class Instances, Memory Arrays & Cache Stride
Executive Summary: Instantiating and managing multiple distinct class objects. We examine memory footprints of multiple instances in SRAM, contiguous array storage, and avoiding duplicate member storage.
💻 1. Annotated Source Code
#include <iostream> #include <string> #include "House.h" using namespace std; int main() { //House myHouse; House theirHouse(2, 10, "green"); theirHouse.print(); //cout << "Before calling any setters..." << endl; //myHouse.print(); ////now call settings //myHouse.setNumStories(2); //myHouse.setNumWindows(6); //myHouse.setColor("red"); //cout << "\nAfter calling setters..." << endl; //myHouse.print(); //House yourHouse; //House myHouse; /*myHouse.setNumStories(2); myHouse.setNumWindows(6); myHouse.setColor("red"); yourHouse.setNumStories(3); yourHouse.setNumWindows(10); yourHouse.setColor("blue"); myHouse.print(); yourHouse.print();*/ return 0; }
#ifndef HOUSE_H #define HOUSE_H #include <string> using namespace std; class House { public: House(); House(int numStories, int numWindows, string color); virtual ~House(); void setNumStories(int numStories); void setNumWindows(int numWindows); void setColor(string color); int getNumStories() const; int getNumWindows() const; string getColor() const; void print() const; private: int numStories; int numWindows; string color; }; #endif
#include "House.h" #include <iostream> using namespace std; House::House() { this->numStories = 1; this->numWindows = 4; this->color = "white"; }//end no-arg ctor House::House(int numStories, int numWindows, string color) { this->numStories = numStories; this->numWindows = numWindows; this->color = color; }//end parameterized ctor House::~House() { cout << "The " << color << " house with " << numStories << " stories" << " and " << numWindows << " windows is being destroyed!" << endl; } void House::setNumStories(int numStories) { this->numStories = numStories; } void House::setNumWindows(int numWindows) { this->numWindows = numWindows; } void House::setColor(string color) { this->color = color; } int House::getNumStories() const { return numStories; } int House::getNumWindows() const { return numWindows; } string House::getColor() const { return color; } void House::print() const { cout << "The house is " << color << " and has " << numStories << " stories and " << numWindows << " windows." << endl; }
📐 2. Architecture & UML Class Model
<<class>>
House
RAII Entity
Attributes / Data Members
-numStories : int32_t
-numWindows : int32_t
-color : std::string
Operations / Methods
+House()
+House(numStories: int, numWindows: int, color: string)
+~House()
+getNumStories() : int32_t const
+setNumStories(stories: int) : void
+getNumWindows() : int32_t const
+setNumWindows(windows: int) : void
+getColor() : std::string const
+setColor(color: string) : void
+printHouse() : void const
📚 3. Core C++ Concepts Deep-Dive
1. Object Independence
Each instantiated object owns its distinct set of non-static member variables in memory. Member functions are shared across all instances in Flash (.text), receiving a hidden this pointer to the target object.
⚡ 4. Embedded Systems & Hardware Reality
1. The this Pointer in Assembly
Under the ARM AAPCS, calling a member function (house.print()) automatically passes the object's memory address as the first argument in register R0 (the this pointer).
💡 5. Production-Ready Embedded Refactoring
Compact building telemetry node array:
💡 Production-Ready Refactor
#include <cstdint> #include <array> struct RoomNode { uint8_t room_id; int16_t temp_centi_celsius; // e.g. 2150 = 21.50 C uint16_t light_lux; }; class SmartBuilding { private: std::array<RoomNode, 8> rooms_{}; public: void update_sensor(uint8_t room_idx, int16_t temp, uint16_t lux) noexcept { if (room_idx < rooms_.size()) { rooms_[room_idx].temp_centi_celsius = temp; rooms_[room_idx].light_lux = lux; } } };
📝 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. Where is the machine code for a class's non-virtual member functions stored in an embedded system?
Detailed Explanation:
Function machine code is stored once in Flash ROM (
.text). Objects in RAM contain only their member variables.
Q2. How is the 'this' pointer passed to member functions in ARM Cortex-M machine code?
Detailed Explanation:
Member functions receive the instance address (
this) as an implicit first parameter in register R0.
Q3. If a class has 3 integer members (12 bytes total) and 10 member functions, what is the sizeof an object instance of this class on a 32-bit MCU?
Detailed Explanation:
Non-virtual member functions add zero size overhead to object instances. The instance size is strictly the sum of its member variables plus any alignment padding.
Q4. What is the memory overhead of having 50 instances of the same C++ class in SRAM?
Detailed Explanation:
Each instance allocates only its private member variable data in SRAM.