This repository explores how C++ manages resources, from doing it right with RAII, to breaking things on purpose, to managing memory without malloc.
Tested on Linux with g++.
- Linux
g++(GCC 10+ recommended)gdb(for the debugging example)
Check:
g++ --version
gdb --versionEach folder is independent.
Compile from the project root or inside each folder.
What it shows
- RAII for files and locks
- No raw
new/delete - Automatic cleanup even when exceptions happen
Build & run
cd raii-logger
g++ -std=c++20 -O2 -pthread main.cpp -o raii_logger
./raii_loggerWhat happens
- Multiple threads write to a log file
- An exception is thrown on purpose
- File and mutex are still cleaned up safely
This demonstrates why RAII prevents leaks and deadlocks.
What it shows
- A classic C/C++ memory bug
- How use-after-free happens
- How to debug it with
gdb
This program is intentionally broken.
Build
cd uaf-gdb
g++ -std=c++20 -g -O0 main.cpp -o uafRun normally
./uafIt may crash or behave strangely that’s the point.
Debug with gdb
gdb ./uafInside gdb:
run
btYou’ll see the crash caused by writing to memory after it was freed.
This project is about learning to recognize and debug undefined behavior.
What it shows
- Manual memory management without
malloc - Fixed-size memory arena
- RAII cleanup of objects
Build & run
cd fixed-arena
g++ -std=c++20 -O2 main.cpp -o fixed_arena
./fixed_arenaWhat happens
- Objects are constructed inside a fixed buffer
- No heap allocation
- Destructors run automatically when the arena resets or goes out of scope
This is similar to patterns used in game engines and embedded systems.
cpp-resource-management/
├── raii-logger/
├── uaf-gdb/
├── fixed-arena/
└── README.md
- Show correct C++ resource management
- Show what goes wrong when ownership is broken
- Show low-level control without dynamic allocation