C++ debugging exercises
In C++, subtle bugs compile cleanly and crash at runtime: modifying a std::vector while iterating over it invalidates pointers, using delete instead of delete[] on an array corrupts heap memory, and std::string::find compared against signed integers breaks on missing substrings.
These hands-on exercises take you through real C++ bugs: memory management, RAII, smart pointers, polymorphic object slicing, and strict weak ordering.
Practise by topic
How to debug a C++ function
- Check iterator invalidation. Any push_back, insert, or erase on a std::vector can reallocate memory and invalidate existing iterators and references.
- Verify new[] matches delete[]. Allocations with new[] must always be paired with delete[], otherwise heap metadata is corrupted.
- Inspect temporary object lifetimes. Returning a std::string_view or const char* to a local std::string creates an immediate dangling pointer.
- Check operator precedence. Bitwise operators (&, |, ^) have lower precedence than comparison operators (==, !=); always wrap bitwise checks in parentheses.
- Verify virtual destructors. Any base class with virtual functions must declare a virtual destructor to prevent resource leaks when deleting derived objects.
The exercises
Easy
Medium
- Vector modification causes unexpected behavior in loopLogic
- Unexpected crash after function exit with lambdaScope and closure
- Resource Leak Detected in Derived Class DestructionScope and closure
- Unexpected behavior in integer-based list traversalLogic
- Unexpected segfault when accessing string dataScope and closure
- Unexpected behavior when passing derived class to baseType
- Array Memory Leak in Dynamic Allocation CleanupLogic
- Unexpected behavior after moving vector elementsLogic
- Unexpected entries in std::map after key lookupsLogic
- Unexpected behavior when returning string from functionScope and closure
- Unexpected crash when processing string view in functionScope and closure
- Resource Leak in Circular std::shared_ptr ReferencesLogic
- Set iteration fails unexpectedly after element modificationLogic
- Vector elements not removed despite using std::removeLogic
- Unexpected behavior in function argument evaluationLogic
- Crash on custom class destruction without copy constructorLogic
- Failed Downcast in Class HierarchyType
- Unexpected behavior when converting char* to int*Type
- Thread synchronization issue causing unexpected behaviorAsync and race conditions
- Unexpected program termination during threaded executionAsync and race conditions
- Memory Corruption in Custom Deleter for std::unique_ptrLogic
- Infinite loop when summing floating point numbersLogic
- std::string::find comparison with signed integerType
- Iterator invalidation in vector erase loopLogic
- Dangling string_view to temporary std::stringScope and closure
- delete vs delete[] mismatch on dynamic arrayLogic
- Object slicing in polymorphic base class pass-by-valueType
- std::map operator[] creates unintended entriesLogic
- std::accumulate integer default truncates float sumType
- Lambda capturing local reference outlives stack frameScope and closure
- Missing virtual destructor causes derived memory leakScope and closure
- Self-assignment in copy assignment operatorLogic
- Use after move on std::unique_ptrNull and undefined
- std::sort comparator violates strict weak orderingLogic
- C-style string buffer overflow with sprintfLogic
- Reverse vector with signed/unsigned comparison issueLogic
- Unsafe dynamic_cast without null validationNull and undefined
- Missing lock_guard unlocks mutex prematurelyAsync and race conditions
- Custom comparator missing const qualifierType
Hard
- Unexpected behavior with static objects in multi-file CPPAsync and race conditions
- Concurrent Counter Increment Causes Data Race on ARMAsync and race conditions
- Member initialization order mismatch with declarationLogic
- Shallow copy of pointer member in custom classLogic
- std::weak_ptr prevents circular reference leakScope and closure
What you will practise
Questions
Which C++ standard is used?
Modern C++ (C++17 and C++20) using standard library containers, smart pointers, lambdas, and algorithms.
Do I need g++ or clang installed?
No. Everything runs through BugHunt's execution environment with zero setup.