C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
A C++ exception wasn’t caught anywhere, so std::terminate aborted the program. what() tells you the reason.
terminate called after throwing an instance ofwhat(): vector::_M_range_checkwhat(): stoi Meaning
Frequent ones: std::out_of_range from .at(), std::invalid_argument from std::stoi on non-numeric text, std::bad_alloc for huge allocations, std::logic_error from basic_string constructed with nullptr.
Common causes
- vector.at()/string.at() with an invalid index
- std::stoi/stod on invalid input
- Allocation failure (bad_alloc)
- No try/catch around code that throws
⚡ Quick fix
- Validate indices and input before calling
- Catch the specific exception where you can recover
- Run in a debugger with catch throw to find the source
Detailed fix by platform
C++
- cpp
try { int n = std::stoi(input); } catch (const std::invalid_argument&) { std::cerr << "not a number\ "; }
How to diagnose
- Type — Exception class and what()
- Source — gdb: catch throw
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in C/C++
- Bus error C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 12 Oct 2026