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

ErrorDescriptionFix
std::bad_allocMemory allocation failed — new cannot allocate requested memoryHandle the exception with try/catch, reduce allocation size, and check for memory leaks
std::out_of_rangeVector or string index out of bounds — accessing beyond the container sizeUse .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