Questions tagged [circular-buffer]

A circular 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.

A circular buffer (AKA cyclic buffer, ring buffer, or circular queue) 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.

The circular buffer uses two pointers to indicate the current beginning and ending of the data on the buffer. The old/"First In" data is deleted from the buffer as it is read, moving forward the beginning pointer. New data is added at the end of the buffer (moving forward the corresponding pointer). The buffer physical storage is the same during all the operation if the reading speed is faster than the data acquisition process.

The useful property of a circular buffer is that it does not need to have its elements shuffled around when one is consumed. (If a non-circular buffer were used then it would be necessary to shift all elements when one is consumed.) In other words, the circular buffer is well suited as a FIFO (first in first out) buffer while a standard, non-circular buffer is well suited as a LIFO (last in first out) buffer.

Circular buffering makes a good implementation strategy for a queue that has fixed maximum size. Should a maximum size be adopted for a queue, then a circular buffer is a completely ideal implementation; all queue operations are constant time. However, expanding a circular buffer requires shifting memory, which is comparatively costly. For arbitrarily expanding queues, a Linked list approach may be preferred instead.

Check http://en.wikipedia.org/wiki/Circular_buffer for more information.

417 questions
0
votes
1 answer

Circular Buffer for Strings

I'm buffering the last X lines of stdout, stderr & stdin of a process. I'd like to keep the last X lines and be able to access a line by its id (line number). So if we store 100 lines and insert 200 of them, you can access lines 100-200. (In reality…
proc
  • 300
  • 3
  • 16
0
votes
0 answers

Circular Queue Bug Fix + Improvement Critiques

The issue is portrayed clearly in the display function.I wanted to display the array items in top, but I couldn't figure out a way to do so as it was constantly being incremented. Instead of this...: void CircularQueue :: Display() const { …
TheRoadLessTaken
  • 531
  • 2
  • 7
  • 15
0
votes
1 answer

Lockless circular buffer with single producer singular consumer

I have a consumer thread that must never lock nor allocate memory, and a producer thread that can. I want to implement a two place circular buffer to be able to provide data to the consumer thread from the producer, with the bound that whenever no…
0
votes
0 answers

What is the most computationally efficient way to retrieve the last item in Apache circularfifobuffer?

What is the most computationally efficient way to retrieve the last item in Apache circularfifobuffer? Last as in the latest to be added. The only way I can see is to cycle through the whole buffer with .remove() or .iterator.next(). (The latter in…
Argyll
  • 8,591
  • 4
  • 25
  • 46
0
votes
2 answers

How to shift one array element wrap around?

I am trying to write a function which iterates through an array and when it finds a certain type of value it will shift it to the right a defined number of places. I know how to shift elements by temporarily storing a value, shifting the right side…
MarkJW
  • 3
  • 1
  • 2
0
votes
1 answer

Filling and saving shared buffer between threads

I'm working with an API that retrieves I/Q data. Calling the function bbGetIQ(m_handle, &pkt);fills a buffer. This is a thread looping while the user hasn't input "stop". Pkt is a structure and the buffer used is pkt.iqData = &m_buffer[0]; which is…
Juju
  • 81
  • 1
  • 9
0
votes
1 answer

Choosing between double buffer and ring buffer?

I have a problem of decoding a packet which is sent through UART of a micro controller (The firmware needs to be baremetal, no RTOS support). The packet is 32 bytes long, and is send at every 10 milliseconds (continuously, without any stop). I need…
SRK
  • 308
  • 3
  • 16
0
votes
1 answer

understanding memory allocation nature for a circular buffer implementation

I am trying to understand the memory allocation while using a third party written Ring Buffer implementation. I referred to two freely available implementations of ring buffer…
tauseef_CuriousGuy
  • 770
  • 1
  • 12
  • 28
0
votes
1 answer

Circular buffer in flash implementation

I have been working on data logger for an embedded device. The goal is to store values of a set of variables in periodic manner into the external flash memory. My idea is to create a buffer in RAM. Size of the buffer will be equal to the number of…
Steve
  • 805
  • 7
  • 27
0
votes
0 answers

Initilizing boost::multi_array with boost::circular_buffer

I would like to create a boost::multi_array of boost::circular_buffer, but I don't know how to initiate it. It should be 12 arrays of boost::circular_buffer of a size 50. I tried: boost::multi_array< boost::circular_buffer ,…
Eagle
  • 3,362
  • 5
  • 34
  • 46
0
votes
1 answer

When would a linked list be preferred over a circular buffer?

In terms of big-O runtime, it seems that both data structures have in the "average" case: O(1) insertion/removal into the start and end O(n) insertion/removal into some arbitrary index O(1) lookup of the start and end Advantages of circular…
meisel
  • 2,151
  • 2
  • 21
  • 38
0
votes
1 answer

Boost ring buffer indexing order, C++

When adding elements to boost/circular_buffer.hpp with push_back, is it possible to assume that the larger index means the later inserted element? More precisely, in my case, to make the recent insertions more significant: double…
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
0
votes
0 answers

Recursive divide and conquer approach to maximum continuous sum on a circular array

I'm implementing a divide and conquer approach to solve the maximum sum problem recursively. I've gotten it down for a regular array but I'm struggling to implement it for a circular array. Right now, I'm trying to use modulus to just go over the…
weztex
  • 53
  • 6
0
votes
0 answers

Ring Buffer/Circular Array Implementation in C

There are a bunch of previous posts and examples for implementing a circular buffer in C, but I many of them include structures and that's a bit too complex for the task which I am trying to do. The task is relatively simple, to print out the last…
nice_remark
  • 325
  • 3
  • 12
0
votes
0 answers

Detecting boundaries and resetting circular buffer pointer in both directions

I am working with an 8051 microcontroller, but my question is more algorithm specific. I have created a circular buffer in memory for random incoming data from external sources. Suppose the buffer is 32 bytes and I received 34 bytes of data. Yes…
Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37