C++ errors range from cryptic template instantiations to hard-to-debug memory corruption. This section covers the most common runtime exceptions with minimal reproducible code examples and concrete fixes.
Error Codes
| Error | Description | Fix |
|---|---|---|
| std::bad_alloc | Memory allocation failed — new cannot allocate requested memory | Handle the exception with try/catch, reduce allocation size, and check for memory leaks |
| std::out_of_range | Vector or string index out of bounds — accessing beyond the container size | Use .at() for bounds-checked access, validate index with .size(), and use iterators |
Quick Debug
# Compile with warnings and sanitizer
g++ -Wall -Wextra -fsanitize=address,undefined -g -o myapp main.cpp
./myapp