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

Populate mysql table with default values

i am looking for a mysql statment to create 50.000 entries in a simple mysql table. The values can just be null or default value, except the primary key(autoincrement). I want to build a circular buffer(see…
devOp
  • 3,150
  • 1
  • 18
  • 32
0
votes
1 answer

Valgrind: Invalid free() on String Array in C

Im codeing a little Ringbuffer in C, everything works, but if i check for memoryleeks, there is the following message: It looks like , the "write_buffer" function causes the leak, but i cant figure out why. valgrind --leak-check=full…
0
votes
0 answers

Trimming time off an insertion sort

I need to frequently add and remove elements to and from a large number of sorted lists. The lists are bound in size, and relatively small -- around 100 elements or less, with each element being on the order of 32 bytes or thereabouts. If inserting…
user1274193
  • 1,104
  • 1
  • 10
  • 11
0
votes
1 answer

boost circular_buffer or heap sorting performance

I need to sort object that arrive to a process X before treating them. Object are sorted given to a time-stamp - a 64 bit number. When object are timed out(a few milli) and sorted, the process X start to look at them. Most of the time the object…
yaron
  • 439
  • 6
  • 16
0
votes
2 answers

Attempt at Circular Buffer - Javascript

Right! Here is my attempt at a circular buffer (For use in a graphing program, using the canvas element). Have yet to get round to testing it out. Question is - Can anyone see any flaws in my logic? Or bottlenecks? /** * A circular buffer class. *…
User2
  • 581
  • 1
  • 7
  • 17
0
votes
2 answers

Is there non-concurrent (other than ArrayBlockingQueue) standard implementation of cyclic queue?

Is there a standard fast Java queue/circular-buffer structure like ArrayBlockingQueue, backed by an array, but without any concurrency synchronisation overhead?
Piotr Müller
  • 5,323
  • 5
  • 55
  • 82
0
votes
1 answer

Circular Queue claims an unretrieved item would be overwritten

class CircularQueue{ private char q[]; private int putloc, getloc; public CircularQueue(int size){ q = new char[size+1]; putloc = getloc = 0; } public void put (char ch) { if (putloc+1==getloc | ((putloc…
Ethan
  • 83
  • 1
  • 2
  • 7
0
votes
2 answers

Bi Directional Circular Arrays Algorithm without pointers

I have an array of the 26 English alphabet characters. Say: char a[26] = ['a','b','c','d'.......so on till ....'z'] I am required to move the elements in the array in a circular manner (could be clockwise or anticlockwise). I understand that there…
Shubham
  • 21
  • 1
  • 2
  • 9
0
votes
1 answer

Is this the most optimal way to shift?

I have the following code for a filter: // Shift array to the left memmove(&Fifo[0], &Fifo[1], 12 * 4); // Add new value Fifo[12] = NewValue; int Result = Factor[0] * Fifo[6]; Result += Factor[1] * (Fifo[5] + Fifo[7]); Result += Factor[2] *…
Maestro
  • 9,046
  • 15
  • 83
  • 116
0
votes
1 answer

Does this circular buffer implementation require a semaphore?

I am implementing the circular buffer as below: long windex = 0, rindex = 0, count = 0; producer_ISR() /* whenever the data avail, then gets the interrupt */ { /* store the data in array buffer */ array[windex] = data; windex++; …
0
votes
2 answers

find the lexicographically minimal string rotation without suffix tree and in O(n)

how to find the index in an circular array such that the string that is formed starting from that index is first in lexicographic order. For Ex : in the circular array ABCDEABCCDE The answer is 6 because the circular string starting from the element…
jairaj
  • 1,789
  • 5
  • 19
  • 32
0
votes
2 answers

Circular buffer using pointers in C

I have a queue structure, that I attempted to implement using a circular buffer, which I am using in a networking application. I am looking for some guidance and feedback. First, let me present the relevant code. typedef struct nwk_packet_type { …
NickHalden
  • 1,469
  • 2
  • 20
  • 31
0
votes
0 answers

Printing a circular buffer

I'm wondering how do I print out this giant circular buffer. The first 3 blocks are structs and the while loop is in a main, this is only part of my code. I'm not focusing the format or the output right now. This is just an idea what I should be…
Tosh
  • 19
  • 1
  • 5
0
votes
1 answer

C - dynamically allocating a circular-buffer of structs within a struct

I am attempting to develop a dynamically-allocated circular-buffer in C using two structs. One holds detailed information and another is essentially used as a pointer from main to the circular-buffer structure (as there will be multiple arrays…
sudo_coffee
  • 888
  • 1
  • 12
  • 26
0
votes
0 answers

Unable to sort out an circular queue ? what is wrong?

I am trying to implement a circular queue that takes a struct ⟶ checks if the queue is full if not enqueues ⟶ else return 0 ⟶ checks if the queue is empty if not dequeues ⟶ else return 0 The following is my code : #ifndef_QUEUE_H_ …
Nodnin
  • 451
  • 2
  • 9
  • 21