Data structures / Circular buffers

Circular buffers

A fixed array used as a never-ending loop. Seven steps follow the head and tail indexes round eight slots: writing, reading, wrapping, filling up and overwriting.

Beginner7 steps~6 min
Slot — one cell of the fixed array
Head — oldest value: reads happen here
Tail — next free slot: writes happen here
New value — written in this step
// THE SHORT VERSION

In 30 seconds

A circular buffer (or ring buffer) is a fixed-size array used as a queue. A tail index says where the next value is written; a head index says where the oldest unread value is.

Both step forward one slot at a time and wrap from the last slot back to the first, which is what the modulo operation does.

When the buffer is full, a new value is either refused or written over the oldest one. Because nothing is moved and no memory is requested, rings are common in audio, logging and streaming.

Circular buffers, step by step

STEPS01 / 07

Names vary: some texts call the write index head and the read index tail, or use “read” and “write”. Sizes are often powers of two so that mod can be done with a fast bit mask.

STEP 01 / 07 A fixed ring of slots
THE MATH
size N = 8 head = 0, tail = 0 count = 0
  1. Step 1: A fixed ring of slots. A circular buffer is an ordinary array whose size is chosen once and never changes. Two indexes travel round it: tail marks the slot the next value will be written to, head marks the oldest value still waiting to be read. Here nothing is stored yet, so both sit at slot 0.
  2. Step 2: Write at the tail. Writing stores the value in the slot tail points to, then moves tail on by one. After A, B and C the tail is at slot 3 and three values are waiting. Nothing else in the array is touched, so a write costs the same however full the buffer is.
  3. Step 3: Read at the head. Reading takes the value at head, the oldest one, and moves head on by one. Two reads return A and then B: first in, first out. Their slots count as free again; the old bytes are not erased, just no longer between head and tail.
  4. Step 4: Wrap round with modulo. D to H fill slots 3 to 7, the end of the array. The next index is worked out modulo the size: (7 + 1) mod 8 = 0, so the tail wraps to the start and I lands in slot 0, which A left free. The array has an end; the ring does not.
  5. Step 5: Full or empty?. One more write, J, fills slot 1 and the tail catches up with the head. But head = tail was also true when the buffer was empty, so the two indexes alone cannot tell full from empty. Common fixes: keep a count, as here, or always leave one slot unused so that a full buffer has tail one step behind head.
  6. Step 6: Overwrite the oldest. What happens on a write to a full buffer is a design choice. An overwriting buffer stores K over the oldest value, C, and pushes head on as well, so the oldest is now D. It always holds the latest 8 values. The other choice is to refuse the write, or make the writer wait, so that nothing is lost.
  7. Step 7: Where rings are used. A ring suits any steady flow between a producer and a consumer: memory use is fixed, nothing is ever shifted, and no memory is requested while it runs. Audio code passes sound samples through one, loggers keep the most recent lines in one, and network or video streams use one to smooth out bursts.
VALUES SO FAR
size8 slotsfixed
write····rule
read····rule
wrap····modulo
empty····test
full····test
lost····overwrite
oldest····head

Spotted a mistake on this page?