Skip to content

Commit e453fdc

Browse files
committed
Add 04-expert memory-management-and-raii module
1 parent 4a682bd commit e453fdc

8 files changed

Lines changed: 217 additions & 8 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Current C++ advanced modules:
4343

4444
- `03-advanced/structs-and-classes`
4545

46+
Current C++ expert modules:
47+
48+
- `04-expert/memory-management-and-raii`
49+
4650
## Guided Learning Path
4751

4852
1. Complete setup: `languages/cpp/00-setup/README.md`
@@ -51,7 +55,7 @@ Current C++ advanced modules:
5155
- read `README.md`
5256
- run `example/main.cpp`
5357
- solve `exercises/01.cpp` and `exercises/02.cpp`
54-
- after foundations, continue with `languages/cpp/02-core`, then `languages/cpp/03-advanced`
58+
- after foundations, continue with `languages/cpp/02-core`, then `languages/cpp/03-advanced`, then `languages/cpp/04-expert`
5559
4. Mark progress in `languages/cpp/CHECKLIST.md`
5660
5. Repeat until all modules are complete
5761

languages/cpp/04-expert/README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# 04 Expert
22

3-
This level is reserved for expert-level C++ topics.
3+
This level focuses on robust C++ design for resource safety and performance.
44

5-
## Intended Topics
5+
## Module Order
66

7-
- Concurrency and synchronization
8-
- Performance and memory optimization
9-
- Large-scale C++ patterns and architecture
7+
1. [memory-management-and-raii](./memory-management-and-raii/README.md)
108

11-
## Status
9+
## How To Study This Level
1210

13-
This level folder is ready for future modules.
11+
For each module:
12+
13+
1. Read `README.md`.
14+
2. Run `example/main.cpp`.
15+
3. Complete `exercises/01.cpp` and `exercises/02.cpp`.
16+
4. Mark progress in `../CHECKLIST.md`.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Memory Management and RAII
2+
3+
This module introduces safe ownership patterns in modern C++.
4+
5+
## Why It Matters
6+
7+
Many C++ bugs come from incorrect memory or resource handling.
8+
RAII and smart pointers reduce those bugs by tying cleanup to object lifetime.
9+
10+
## Quick Run
11+
12+
```bash
13+
g++ -std=c++17 -Wall -Wextra -pedantic example/main.cpp -o memory_raii_example
14+
./memory_raii_example
15+
```
16+
17+
On Windows (MSYS2 shell), run:
18+
19+
```bash
20+
./memory_raii_example.exe
21+
```
22+
23+
## Topics Covered
24+
25+
### Stack vs Heap
26+
27+
- Stack objects are destroyed automatically at scope end.
28+
- Heap objects need ownership and cleanup strategy.
29+
30+
### RAII (Resource Acquisition Is Initialization)
31+
32+
Acquire resources in constructors and release them in destructors.
33+
34+
### Smart Pointers
35+
36+
- `std::unique_ptr<T>`: single owner.
37+
- `std::shared_ptr<T>`: shared ownership (use carefully).
38+
- Prefer `unique_ptr` unless shared ownership is required.
39+
40+
## Common Pitfalls
41+
42+
- Using raw `new`/`delete` directly in beginner/intermediate code.
43+
- Copying `unique_ptr` (it must be moved).
44+
- Creating ownership cycles with `shared_ptr`.
45+
46+
## Exercise Focus
47+
48+
- `exercises/01.cpp`: use `std::unique_ptr<int[]>` for dynamic arrays safely.
49+
- `exercises/02.cpp`: model RAII with a scope-based guard class.
50+
51+
## Checkpoint
52+
53+
- [ ] I can explain ownership with `unique_ptr`.
54+
- [ ] I understand how destructors enforce cleanup.
55+
- [ ] I can model scope-based resource management.
56+
- [ ] I completed both exercises.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <cstddef>
2+
#include <iostream>
3+
#include <memory>
4+
#include <string>
5+
6+
class ScopedMessage {
7+
public:
8+
explicit ScopedMessage(const std::string& labelText) : label(labelText) {
9+
std::cout << "[acquire] " << label << '\n';
10+
}
11+
12+
~ScopedMessage() {
13+
std::cout << "[release] " << label << '\n';
14+
}
15+
16+
private:
17+
std::string label;
18+
};
19+
20+
std::unique_ptr<int[]> makeSequence(std::size_t size) {
21+
std::unique_ptr<int[]> data(new int[size]);
22+
for (std::size_t i = 0; i < size; ++i) {
23+
data[i] = static_cast<int>((i + 1) * 10);
24+
}
25+
return data;
26+
}
27+
28+
void printSequence(const std::unique_ptr<int[]>& data, std::size_t size) {
29+
std::cout << "Sequence: ";
30+
for (std::size_t i = 0; i < size; ++i) {
31+
std::cout << data[i];
32+
if (i + 1 < size) {
33+
std::cout << ' ';
34+
}
35+
}
36+
std::cout << '\n';
37+
}
38+
39+
int main() {
40+
std::cout << "RAII scope demo:\n";
41+
{
42+
ScopedMessage scoped("temporary operation");
43+
std::cout << "Inside scope.\n";
44+
}
45+
46+
const std::size_t size = 5;
47+
std::unique_ptr<int[]> values = makeSequence(size);
48+
printSequence(values, size);
49+
50+
std::cout << "Ownership transfer with move:\n";
51+
std::unique_ptr<int[]> movedValues = std::move(values);
52+
if (!values) {
53+
std::cout << "Original pointer is now empty after move.\n";
54+
}
55+
printSequence(movedValues, size);
56+
57+
return 0;
58+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <cstddef>
2+
#include <iostream>
3+
#include <memory>
4+
5+
int main() {
6+
int n = 0;
7+
std::cout << "How many integers? ";
8+
std::cin >> n;
9+
10+
if (n <= 0) {
11+
std::cout << "Please enter a positive count.\n";
12+
return 0;
13+
}
14+
15+
std::unique_ptr<int[]> values(new int[static_cast<std::size_t>(n)]);
16+
17+
for (int i = 0; i < n; ++i) {
18+
std::cout << "Value " << (i + 1) << ": ";
19+
std::cin >> values[static_cast<std::size_t>(i)];
20+
}
21+
22+
long long sum = 0;
23+
for (int i = 0; i < n; ++i) {
24+
sum += values[static_cast<std::size_t>(i)];
25+
}
26+
27+
std::cout << "Sum: " << sum << '\n';
28+
std::cout << "Reversed: ";
29+
for (int i = n - 1; i >= 0; --i) {
30+
std::cout << values[static_cast<std::size_t>(i)];
31+
if (i > 0) {
32+
std::cout << ' ';
33+
}
34+
}
35+
std::cout << '\n';
36+
37+
return 0;
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
class CounterGuard {
5+
public:
6+
explicit CounterGuard(int& sharedCounterRef, const std::string& labelText)
7+
: sharedCounter(sharedCounterRef), label(labelText) {
8+
++sharedCounter;
9+
std::cout << "[enter] " << label << " | active guards: " << sharedCounter << '\n';
10+
}
11+
12+
~CounterGuard() {
13+
--sharedCounter;
14+
std::cout << "[exit] " << label << " | active guards: " << sharedCounter << '\n';
15+
}
16+
17+
private:
18+
int& sharedCounter;
19+
std::string label;
20+
};
21+
22+
int main() {
23+
int activeGuards = 0;
24+
25+
std::cout << "Starting scope demo.\n";
26+
{
27+
CounterGuard first(activeGuards, "first scope");
28+
{
29+
CounterGuard second(activeGuards, "nested scope");
30+
std::cout << "Inside nested scope.\n";
31+
}
32+
std::cout << "Back to first scope.\n";
33+
}
34+
35+
std::cout << "Final active guards: " << activeGuards << '\n';
36+
return 0;
37+
}

languages/cpp/CHECKLIST.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@
2929

3030
- [ ] structs-and-classes: model entities with constructors and encapsulation.
3131
- [ ] structs-and-classes exercises completed.
32+
33+
## Expert
34+
35+
- [ ] memory-management-and-raii: apply ownership rules and scope-based cleanup.
36+
- [ ] memory-management-and-raii exercises completed.

languages/cpp/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Current concept modules inside `03-advanced`:
3535

3636
- [structs-and-classes](./03-advanced/structs-and-classes/README.md)
3737

38+
Current concept modules inside `04-expert`:
39+
40+
- [memory-management-and-raii](./04-expert/memory-management-and-raii/README.md)
41+
3842
Use the checklist: [CHECKLIST.md](./CHECKLIST.md)
3943

4044
## Concept Folder Structure
@@ -74,6 +78,10 @@ Then continue with `03-advanced`:
7478

7579
1. `structs-and-classes`
7680

81+
Then continue with `04-expert`:
82+
83+
1. `memory-management-and-raii`
84+
7785
## Build Command
7886

7987
```bash

0 commit comments

Comments
 (0)