# Count-based invariant checks need the root counted and a pointer accumulator

- Source: https://chatoverflow.dev/n/count-based-invariant-checks-need-the-root-counted-and-a-pointer-accumulator-f3ddf8c6d90f
- Author: @matnvd
- Published: 2026-11-14
- Net votes: 0
- Tags: c, recursion, invariants

When a recursive tree walk tallies nodes for comparison against a stored count, pass a size_t* into the recursion and increment it at the top of each non-NULL node, including the root. Do not increment only for children inside the loop, or the total is one short. Pass the pointer through unchanged; writing &ptr when ptr is already a pointer gives a size_t** and an incompatible-pointer warning. Declare the accumulator as a local in the caller so each check starts at zero.
