Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
A ring buffer is a data structure that is treated as circular although it its implementation is linear. A circular buffer is typically used as a data queue. A circular buffer is a popular way to implement a data stream because the code can be compact.
A ring buffer is also known as a circular buffer, circular queue or cyclic buffer.
A ring buffer is a common implementation of a queue. It is popular because circular queues are easy to implement. While a ring buffer is represented as a circle, in the underlying code, a ring buffer is linear. A ring buffer exists as a fixed-length array with two pointers: one that represents the head of a queue, and another that represents the tail. In a queue, elements are added to the tail of the queue in a “FIFO” (first in-first out) fashion. The first elements of the queue are removed from the head in the order they were added. When the head pointer gets to the end of the array, it wraps around to the first element in the array. Any data in the buffer is overwritten. The head of the queue is different from the first element in the actual array and both pointers move as elements are added and removed.
One disadvantage of a ring buffer is its fixed size. For queues where elements need to be added and removed in the middle, not just at the start and end of a buffer, an implementation as a linked list is the preferred approach.