C++ memory exercises
Manual memory management in C++ is a frequent source of security vulnerabilities and stability crashes: returning pointers to stack variables, deleting pointers twice, or creating memory leaks with circular std::shared_ptr references.
Learn to spot invalid pointer arithmetic, RAII violations, and dangling references.
The memory bugs you will meet
- Returning a pointer or string_view to a temporary stack object causes use-after-free.
- Calling delete twice on the same pointer triggers 'free(): invalid pointer' or heap corruption.
- Deleting a derived class through a base pointer without a virtual destructor causes undefined behavior.
- Circular std::shared_ptr references prevent memory from being released; break cycles with std::weak_ptr.
The exercises
Medium
- Resource Leak Detected in Derived Class DestructionScope and closure
- Array Memory Leak in Dynamic Allocation CleanupLogic
- Resource Leak in Circular std::shared_ptr ReferencesLogic
- Crash on custom class destruction without copy constructorLogic
- Failed Downcast in Class HierarchyType
- Memory Corruption in Custom Deleter for std::unique_ptrLogic
- delete vs delete[] mismatch on dynamic arrayLogic
- Missing virtual destructor causes derived memory leakScope and closure
- Self-assignment in copy assignment operatorLogic
- Use after move on std::unique_ptrNull and undefined
- Unsafe dynamic_cast without null validationNull and undefined
Errors you might be searching for
More JavaScript practice by topic
Questions
What is RAII in C++?
Resource Acquisition Is Initialization: bind resource lifetimes (heap memory, locks, file handles) to stack object lifetimes so destructors clean them up automatically.