Sub topic 5
The full-versus-empty problem
This chapter assumes you know zero-based array indexing, the FIFO queue operations (enqueue and dequeue), the modulo operator, and basic Python or C syntax. It also assumes you have seen the head and tail indices from the previous chapters.
Where the problem comes from
A ring buffer keeps two indices into a fixed array:
headis the index of the oldest item. Pop reads here.tailis the index where the next item will be written. Push writes here.
Both indices advance with wraparound: (index + 1) % capacity. Push moves tail forward. Pop moves head forward.
An empty buffer has nothing between the two indices, so head == tail. Now push until every slot is used. The tail advances capacity times and wraps back to where it started. A full buffer also has head == tail.
CAP = 4
buf = [None] * CAP
head = 0
tail = 0
print("empty:", head, tail) # empty: 0 0
for item in range(CAP):
buf[tail] = item
tail = (tail + 1) % CAP
print("full:", head, tail) # full: 0 0The two printed lines are identical. Looking only at head and tail, you cannot tell a buffer holding nothing from one holding four items.
Why the ambiguity is a real bug
A push needs to know whether the buffer is full, and a pop needs to know whether it is empty. Both checks look at the same condition, head == tail, and it can only be given one meaning.
- If you treat
head == tailas empty, a full buffer looks empty. The next pop returns stale data, and the next push overwrites the oldest item. - If you treat
head == tailas full, a new empty buffer looks full and rejects every push.
Either choice fails in one of the two states. The bug often survives light testing, because short sequences of pushes and pops rarely fill the buffer exactly.
The cause: too few distinct states
The indices each take capacity values, from 0 to capacity - 1. The buffer can hold anywhere from 0 to capacity items, which is capacity + 1 possible fill levels. Two indices that wrap can only distinguish capacity positions of tail relative to head. Levels 0 and capacity collapse into the same index difference.
| Items stored | Distance from head to tail (mod capacity) |
|---|---|
| 0 | 0 |
| 1 | 1 |
| `capacity - 1` | `capacity - 1` |
| `capacity` | 0 |
Any fix must add information, or give up one of the states. The next chapter compares three ways to do that. For now, remember that the information has to come from somewhere other than head and tail taken modulo the capacity.
Common mistakes
- Checking
head == tailfor both conditions without any extra state. - Fixing the bug for push but not pop, so one operation is guarded and the other is not.
- Testing only with a buffer that never reaches capacity, or that only fills right after creation.
- Assuming that
tail - headgives the count. After wraparound it can be negative, and when the buffer is full it is 0.
Exercise
A ring buffer has capacity 5, and only head and tail are stored. After a series of operations, head is 2 and tail is 2. How many items could the buffer hold, and why?
Show answer
It could hold 0 items or 5 items. Both states leave tail at the same position as head after wrapping, so the two indices alone cannot show which one it is. You need extra information, such as a count field.