# C/C++: Assertion `x > 0' failed. Aborted (core dumped) — assert() condition was false

**Code:** `Assertion failed` · **Category:** C/C++ · **Seen on:** C/C++, Linux, GCC, Clang

An `assert()` in your code or in a library found its condition false and aborted the program on purpose.

## 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_ASSERTIONS` or in a debug STL
- A moved-from or uninitialised object was used
- Side effects hidden inside `assert(...)` that behave differently with `NDEBUG`
- A library's internal consistency check failed after earlier memory corruption

## Quick fix

1. Read the expression and file:line in the message and trace who supplied the bad value
2. Run under `gdb ./app` and use `bt` after the abort
3. Never put code with side effects inside `assert`

## Detailed fix by platform

### Linux

1. `gdb ./app` then `run` and `bt` shows the call chain that reached the assert
2. `ulimit -c unlimited` to get a core file for post-mortem analysis

### GCC / Clang

1. Build debug with `-g -O0`; keep asserts on in tests and remove `-DNDEBUG` from the test configuration

## Code examples

### Assert and a real check

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

1. **Expression** — Which condition failed (file:line)?
2. **Caller** — Which call supplied the bad value?
3. **Build** — Debug or release (NDEBUG)?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/assertion-failed-aborted)
