TryBugHunt

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.

See the exercisesDo it as a 30-day pathFilter and search

Practise by topic

How to debug a C++ function

  1. Check iterator invalidation. Any push_back, insert, or erase on a std::vector can reallocate memory and invalidate existing iterators and references.
  2. Verify new[] matches delete[]. Allocations with new[] must always be paired with delete[], otherwise heap metadata is corrupted.
  3. Inspect temporary object lifetimes. Returning a std::string_view or const char* to a local std::string creates an immediate dangling pointer.
  4. Check operator precedence. Bitwise operators (&, |, ^) have lower precedence than comparison operators (==, !=); always wrap bitwise checks in parentheses.
  5. Verify virtual destructors. Any base class with virtual functions must declare a virtual destructor to prevent resource leaks when deleting derived objects.

The exercises

Medium

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.