C++: terminate called after throwing an instance of 'std::bad_alloc' — what(): std::bad_alloc
new or a container could not get the memory requested — either the system is out of memory or the size was absurdly large (often a negative or uninitialised value).
what(): std::bad_allocterminate called after throwing an instance of 'std::bad_alloc'std::length_error vector::reservecannot allocate memory Meaning
A bad_alloc is thrown by operator new when allocation fails. Many cases are not a real shortage: an int size that became huge after a signed/unsigned conversion, an uninitialised variable passed to std::vector(n), or an infinite loop adding elements. Also check limits like ulimit -v, container memory limits, and 32-bit address space.
Common causes
- Size computed from an uninitialised or negative number (converted to a huge
size_t) - Real memory exhaustion: unbounded growth, leaks
- Container or
ulimit -vmemory limit (Docker, cgroups) - 32-bit build hitting the 2–4 GB address-space limit
newof an enormous array
⚡ Quick fix
- Print the requested size before allocating and validate it
- Catch
std::bad_allocwhere you can degrade gracefully - Check
free -h,ulimit -aand container limits
Detailed fix by platform
Linux
ulimit -v(address-space limit) andfree -h; in Docker checkdocker statsand the--memorysetting- Run under valgrind or AddressSanitizer to find leaks
C++
- Validate input sizes; use
reserve()carefully and prefer streaming over loading whole files
Code examples
Guard the size
#include <vector>
#include <new>
#include <iostream>
int main(int argc, char** argv) {
long long n = argc > 1 ? std::atoll(argv[1]) : -1; // -1 becomes a huge size_t below
if (n < 0 || n > 100'000'000) { std::cerr << "bad size\n"; return 1; }
try {
std::vector<int> v(static_cast<std::size_t>(n));
} catch (const std::bad_alloc& e) {
std::cerr << "out of memory: " << e.what() << "\n";
}
}How to diagnose
- Size — What size was requested?
- Memory — Free memory and limits?
- Growth — Does memory keep growing (leak)?
🔧 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
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- Bus error C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file
Most viewed in C/C++
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
- format expects argument of type C: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
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 15 Oct 2026