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
2 answers

using toString to print Circular Queue

I am trying to find an easier way to print the circular array queue. Here is what I have so far. public class CircularArrayQueue implements QueueADT { private final static int DEFAULT_CAPACITY = 100; protected int…
0
votes
0 answers

Are lockless ring-buffers technically possible?

I need to implement a lockless ring-buffer but I still haven't found any way of doing it without a kind of lock. I've searched the internet and all I found are ways using bits to say the memory is locked. So, it is technically possible to do it…
0
votes
0 answers

How to declare a boost::circular_buffer to be initialized later?

I'm trying to use a circular buffer on disk, but to simplify my question I'm trying to use a circular buffer in different functions without having to pass the circular buffer as an argument. So it may look like this: #include…
0
votes
1 answer

How can I shift a signal by 90 degrees using a circular buffer

I am trying to program a DSP (TMSF28335) using writing C codes in Control Studio V6.02. In this project, I need to make a 90 degrees phase offset on an AC signal that I measure by the sensors. I was advised to use a circular buffer in order to make…
0
votes
1 answer

boost circular_buffer holding pointers

is there a way i can ensure my object gets deleted before it is overwritten in a the circular buffer? here is some code to illustrate my question. boost::circular_buffer cBuf(5); cBuf.push_back(new MyObject()); //…
mike
  • 167
  • 2
  • 6
0
votes
1 answer

Efficient reformatting of ring buffer (circular buffer) to continous array

I've successfully implemented a function that copies an arbitrary amount of values starting from an arbitrary point in a ring buffer to a continuous array but I would like to make it more efficient. Here's a minimum example of my code: #include…
JohanPI
  • 161
  • 9
0
votes
2 answers

Send message through Ring (Circular) Buffer between Threads (in C)

I need to send a message from Main thread to my Created Thread using WinAPI and Ring Buffer. I defined structures and wrote functions for my Ring buffer. Ring Buffer - it contains head, tail, size and pointer to the structure Descriptor which has…
0
votes
1 answer

C++ Using allocator and initializer with boost circular buffer

I am trying to figure out how to use the following constructor for boost::circular_buffer: circular_buffer(capacity_type buffer_capacity, size_type n, const_reference item, const allocator_type& alloc = allocator_type()); I have a custom…
arias_JC
  • 549
  • 3
  • 15
0
votes
1 answer

C++ circular array queue setCapacity

I am working on an assignment that involves me writing a template class for a queue. It uses a dynamically allocated array. I am having trouble with the copying of the array. This is the prompt. aQueue.setCapacity(newCapacity), that changes the…
0
votes
2 answers

Calculating a running average

The running average calculated from circular array produced constant < 0 average when average should always be between 0 and 1. I am writing firmware for an MSP430 device that uses LEDs and photodiodes to detect specific types on ink. The device…
Kyle G
  • 71
  • 10
0
votes
2 answers

Circular Array Stuck in if statements

I am writing firmware for an MSP430 device that uses LEDs and photodiodes to detect specific types on ink. The device scans at about 155us and the samples under the scanner range from velocities of .1m/s to 3.3m/s. The goal of the device is to…
Kyle G
  • 1
  • 3
0
votes
1 answer

How to implement circular buffer in Objective C for high performance

We want to add an array of doubles to a circular buffer in Objective C many times a second. We are currently using a NSMutableArray nested within another NSMutableArray (2D array). This works fine but is too slow for our needs. We want to add to the…
0
votes
1 answer

circular array backed queue resize/enqueue

I'm trying to finish this last method for the circular array backed queue. I'm stuck on the resize method. Any help or pointers are appreciated. def resize(self, newsize): assert(len(self.data) < newsize) new = Queue(newsize) for x…
0
votes
2 answers

Returning struct from function defined in thread

I have a struct defined like typedef struct{ int op; int id; int val; } command_t; command_t cmd_buffer[10]; I am creating a bunch of threads to work with a struct array in a circular buffer. In the thread creaction I call the function…
spacing
  • 730
  • 2
  • 10
  • 41
0
votes
0 answers

What is a real life example of a ring buffer/circular queue?

These are some examples I could think off but I don't think they are perfect:- Conga Line(Dance):- Not perfect because people enter at the end of the circular queue but exit randomly. (People just leave randomly not in a serial fashion) Airport…
david419
  • 435
  • 3
  • 8
  • 18