encyclopegia.org

circular buffer

In computer science, a circular buffer, circular queue, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams. There were early circular buffer implementations in hardware.

In computer science, a circular buffer, circular queue, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams. There were early circular buffer implementations in hardware. This image shows a partially full buffer with Length = 7: This image shows a full buffer with four elements (numbers 1 through 4) having been overwritten: In the beginning the indexes end and start are set to 0. The circular buffer write operation writes an element to the end index position and the end index is incremented to the next buffer position. The circular buffer read operation reads an element from the start index position and the start index is incremented to the next buffer position. The start and end indexes alone are not enough to distinguish between buffer full or empty state while also utilizing all buffer slots, but can be if the buffer only has a maximum in-use size of Length − 1. In this case, the buffer is empty if the start and end indexes are equal and full when the in-use size is Length − 1. Another solution is to have another integer count that is incremented at a write operation and decremented at a read operation. Then checking for emptiness means testing count equals 0 and checking for fullness means testing count equals Length. The following source code is a minimal C++ implementation. push() puts an item in the buffer, pop() gets an item from the buffer. Both functions take care about the capacity of the buffer: import std; using std::array; using std::runtime_error; template class CircularBuffer { private: array data{}; size_t head = 0; size_t tail = 0; size_t size = 0; public: bool isEmpty() const noexcept { return size == 0; } bool isFull() const noexcept { return size == N; } size_t getSize() const noexcept { return size; } void push(const T& value) { if (isFull()) { throw runtime_error("Buffer is full"); } data[tail] = value; tail = (tail + 1) % N; ++size; } T pop() { if (isEmpty()) { throw runtime_error("Buffer is empty"); } T value = data[head]; head = (head + 1) % N; --size; return value; } T peek() const { if (isEmpty()) { throw runtime_error("Buffer is empty"); } return data[head]; } }; int main() { CircularBuffer buffer; buffer.push(1); buffer.push(2); buffer.push(3); std::println("{}", buffer.pop()); // prints 1 buffer.push(4); while (!buffer.isEmpty()) { std::println("{}", buffer.pop()); } }

References

No reference rows published for this article.

Cite this article

MLA

"circular buffer." encyclopegia.org, 2026, https://encyclopegia.org/wiki/circular-buffer. Accessed 24 November 2026.

Chicago

"circular buffer." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/circular-buffer.

APA

encyclopegia.org. (2026). circular buffer. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/circular-buffer

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · tier1-factual · https://en.wikipedia.org/w/index.php?oldid=1373518894 · 0.55 · snapshot circular-buffer-142306-86cdc8a45ef06331 · QID Q1224994

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.