C/C++: Assertion `x > 0' failed. Aborted (core dumped) — assert() condition was false
An assert() in your code or in a library found its condition false and aborted the program on purpose.
Assertion failedAssertion `n > 0' failedAssertion failed: (x), function main, file main.cAssertion `__n < this->size()' failed Meaning
assert(cond) documents something that must always be true. The message prints the file, line, function and the expression, so the bug is the broken precondition, not the assert itself. Asserts are removed by -DNDEBUG in release builds, so the same bug may appear as silent corruption there. Hardened standard libraries (-D_GLIBCXX_ASSERTIONS) assert on out-of-range container access too.
Common causes
- A caller passed a value that violates the function's precondition
- Index out of range on a container built with
_GLIBCXX_ASSERTIONSor in a debug STL - A moved-from or uninitialised object was used
- Side effects hidden inside
assert(...)that behave differently withNDEBUG - A library's internal consistency check failed after earlier memory corruption
⚡ Quick fix
- Read the expression and file:line in the message and trace who supplied the bad value
- Run under
gdb ./appand usebtafter the abort - Never put code with side effects inside
assert
Detailed fix by platform
Linux
gdb ./appthenrunandbtshows the call chain that reached the assertulimit -c unlimitedto get a core file for post-mortem analysis
GCC / Clang
- Build debug with
-g -O0; keep asserts on in tests and remove-DNDEBUGfrom the test configuration
Code examples
Assert and a real check
#include <cassert>
#include <vector>
int pick(const std::vector<int>& v, std::size_t i) {
assert(i < v.size() && "index out of range"); // aborts in debug builds
if (i >= v.size()) return -1; // keep a real check for release builds / user input
return v[i];
}How to diagnose
- Expression — Which condition failed (file:line)?
- Caller — Which call supplied the bad value?
- Build — Debug or release (NDEBUG)?
🔧 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
- call of overloaded is ambiguous C++: error: call of overloaded 'f(int)' is ambiguous
- cannot declare variable to be of abstract type C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions)
Most viewed in C/C++
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- 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
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