std::bad_alloc C/C++ Origin: Application & API Last updated 15 Oct 2026

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).

Exact message you may see what(): std::bad_allocterminate called after throwing an instance of 'std::bad_alloc'std::length_error vector::reservecannot allocate memory
Seen on: C/C++ GCC Clang

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 -v memory limit (Docker, cgroups)
  • 32-bit build hitting the 2–4 GB address-space limit
  • new of an enormous array

⚡ Quick fix

  1. Print the requested size before allocating and validate it
  2. Catch std::bad_alloc where you can degrade gracefully
  3. Check free -h, ulimit -a and container limits

Detailed fix by platform

Linux

  1. ulimit -v (address-space limit) and free -h; in Docker check docker stats and the --memory setting
  2. Run under valgrind or AddressSanitizer to find leaks

C++

  1. Validate input sizes; use reserve() carefully and prefer streaming over loading whole files

Code examples

Guard the size

cpp
#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

  1. Size — What size was requested?
  2. Memory — Free memory and limits?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

AddressSanitizer: heap-buffer-overflow — C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N (C/C++)AddressSanitizer: hea…AddressSanitizer: heap-use-after-free — C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free (C/C++)AddressSanitizer: hea…Bus error — C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file (C/C++)Bus errordouble free or corruption — C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped) (C/C++)double free or corrup…LeakSanitizer: detected memory leaks — C/C++: ==ERROR: LeakSanitizer: detected memory leaks — Direct leak of N byte(s) (C/C++)LeakSanitizer: detect…Segmentation fault — C/C++: Segmentation fault (core dumped) — SIGSEGV from a null, dangling or out-of-bounds pointer (C/C++)Segmentation faultterminate called after throwing an instance of — C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check (C/C++)terminate called afte…terminate called without an active exception — C++: terminate called without an active exception — std::thread destroyed while joinable (C/C++)terminate called with…std::bad_…

Explore the map of this error Full error map →

🧠 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.