A circular buffer is a fixed-size array treated as if its ends are joined into a ring. Two indices — head (read) and tail (write) — advance and wrap around using modulo arithmetic, giving an O(1) FIFO queue with no allocation after creation.
How wrapping works
text
capacity 5, after writing A,B,C,D and reading A,B:
index: 0 1 2 3 4
[ . ][ . ][ C ][ D ][ . ]
head^ tail^ (tail wraps to 0 next write)
