Teaching Notes: Module 01
Key Learning Objectives
Section titled “Key Learning Objectives”- Stack vs Heap allocation
newanddeleteoperators- References vs Pointers
- Pointers to member functions
switchstatement
Exercise 00: BraiiiiiiinnnzzzZ
Section titled “Exercise 00: BraiiiiiiinnnzzzZ”What It Tests
Section titled “What It Tests”- Class design with constructor/destructor
newfor heap allocation- Understanding when to use stack vs heap
Common Mistakes
Section titled “Common Mistakes”-
Not understanding when to use heap
newZombie()returns a pointer - must use heap (survives function)randomChump()uses stack (destroyed at end of function)
-
Memory leaks
Zombie* z = newZombie("Bob");// Student forgets: delete z; -
Destructor message missing
- Subject requires destructor to print zombie name
Guiding Questions
Section titled “Guiding Questions”- “If you create a zombie inside a function and return it, will it still exist outside?”
- “What happens to stack variables when a function ends?”
- “How do you free memory allocated with
new?”
Exercise 01: Moar brainz!
Section titled “Exercise 01: Moar brainz!”What It Tests
Section titled “What It Tests”- Array allocation with
new[] - Proper deallocation with
delete[] - Placement new (optional advanced concept)
Common Mistakes
Section titled “Common Mistakes”-
Using
deleteinstead ofdelete[]Zombie* horde = new Zombie[N];delete horde; // WRONG!delete[] horde; // CORRECT -
Not initializing zombie names
- Must set name for each zombie in the array
-
Returning local array
Zombie* zombieHorde(int N, std::string name) {Zombie horde[N]; // WRONG - stack array, dies at returnreturn horde;}
Guiding Questions
Section titled “Guiding Questions”- “What’s the difference between
deleteanddelete[]?” - “How do you set the name of each zombie in an array?”
Exercise 02: HI THIS IS BRAIN
Section titled “Exercise 02: HI THIS IS BRAIN”What It Tests
Section titled “What It Tests”- Understanding that references are aliases
- Address comparison between variable, pointer, and reference
Key Insight
Section titled “Key Insight”All three should print the same address and value:
std::string str = "HI THIS IS BRAIN";std::string* stringPTR = &str;std::string& stringREF = str;
&str == stringPTR == &stringREF // All same addressstr == *stringPTR == stringREF // All same valueCommon Mistakes
Section titled “Common Mistakes”- Confusing
&(address-of) with&(reference declaration) - Not understanding that stringREF IS str, not a copy
Exercise 03: Unnecessary violence
Section titled “Exercise 03: Unnecessary violence”What It Tests
Section titled “What It Tests”- When to use reference vs pointer as class member
- Understanding reference initialization requirements
Key Decision Point
Section titled “Key Decision Point”| HumanA | HumanB |
|---|---|
| Always has weapon | May not have weapon |
| Weapon in constructor | Weapon set later |
| Use reference | Use pointer |
Common Mistakes
Section titled “Common Mistakes”-
Using pointer for HumanA
- Works but misses the point of the exercise
-
Reference not initialized in constructor
class HumanA {Weapon& _weapon; // Must be initialized!public:HumanA(std::string name) {} // ERROR: _weapon not initialized}; -
Not checking NULL for HumanB
void HumanB::attack() {// Must check if _weapon is NULL before using}
Guiding Questions
Section titled “Guiding Questions”- “Can a reference be NULL?”
- “Can a reference be changed to refer to something else after initialization?”
- “If HumanB might not have a weapon, what type should we use?”
Exercise 04: Sed is for losers
Section titled “Exercise 04: Sed is for losers”What It Tests
Section titled “What It Tests”- File I/O with ifstream/ofstream
- String manipulation without
std::string::replace - Creating output file with
.replaceextension
Common Mistakes
Section titled “Common Mistakes”-
Using forbidden
std::string::replace- Must use find + substr + concatenation
-
Not handling file errors
std::ifstream file(filename);if (!file.is_open()) {// Must handle this!} -
Not handling multiple occurrences
- Must replace ALL occurrences, not just first
Implementation Hint
Section titled “Implementation Hint”std::string replaceAll(std::string str, const std::string& from, const std::string& to) { size_t pos = 0; while ((pos = str.find(from, pos)) != std::string::npos) { str = str.substr(0, pos) + to + str.substr(pos + from.length()); pos += to.length(); } return str;}Exercise 05: Harl 2.0
Section titled “Exercise 05: Harl 2.0”What It Tests
Section titled “What It Tests”- Pointers to member functions
- Avoiding if/else chains
The Pattern
Section titled “The Pattern”void (Harl::*funcs[4])() = { &Harl::debug, &Harl::info, &Harl::warning, &Harl::error};
std::string levels[4] = {"DEBUG", "INFO", "WARNING", "ERROR"};
for (int i = 0; i < 4; i++) { if (level == levels[i]) { (this->*funcs[i])(); return; }}Common Mistakes
Section titled “Common Mistakes”-
Using if/else forest
- Subject explicitly forbids this
-
Wrong syntax for calling member function pointer
funcs[i](); // WRONG(this->*funcs[i])(); // CORRECT
Exercise 06: Harl filter
Section titled “Exercise 06: Harl filter”What It Tests
Section titled “What It Tests”switchstatement- Fall-through behavior
Key Pattern
Section titled “Key Pattern”switch (level) { case DEBUG: debug(); // NO BREAK - fall through! case INFO: info(); case WARNING: warning(); case ERROR: error(); break; default: std::cout << "[ Probably complaining about insignificant problems ]" << std::endl;}Common Mistakes
Section titled “Common Mistakes”-
Adding break to each case
- Subject wants fall-through for filtering
-
Using strings in switch
- C++98 doesn’t support switch on strings
- Must convert to int/enum first
Guiding Questions
Section titled “Guiding Questions”- “What happens if you don’t put
breakin a switch case?” - “Can you switch on a string in C++98?”
General Tips for Module 01
Section titled “General Tips for Module 01”Memory Visualization
Section titled “Memory Visualization”Draw diagrams showing:
- Stack frame with local variables
- Heap with allocated memory
- Pointers as arrows to memory locations
- References as alternative names for same memory
Common Conceptual Struggles
Section titled “Common Conceptual Struggles”-
“Why can’t references be NULL?”
- Reference is an alias, must refer to something
- Pointer is an address, can be “no address” (NULL)
-
“Why use references at all?”
- Cleaner syntax (no
*to dereference) - Cannot be null (one less error case)
- Cannot be reassigned (makes intent clear)
- Cleaner syntax (no
-
“When stack, when heap?”
- Stack: Known size, dies with scope, fast
- Heap: Dynamic size, manual lifetime, returned from functions