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

Circular-/Ring-Buffer with blocking read and non-blocking write?

I am searching for a ringbuffer-implementation in C in userspace, so I can use it in my library. Because I need a ringbuffer with non-blocked write (=overwrite oldest data) blocked read if empty I searched a while and remembered I have used…
Martin L.
  • 3,006
  • 6
  • 36
  • 60
0
votes
0 answers

Use non-blocking FIFO as a ring-buffer?

I have created a FIFO where I can do non-blocking writes into in this way: // others, searching for a non-blocking FIFO-writer may copy this ;-) mkfifo("/tmp/myfifo", S_IRWXU); int fifo_fd = open("/tmp/myfifo", O_RDWR); fcntl(fifo_fd, F_SETFL,…
Martin L.
  • 3,006
  • 6
  • 36
  • 60
0
votes
1 answer

iOS UI are causing a glitch in my audio stream

I'm writing a VOIP based app for the iPhone. I'm having a strange issue where when the user presses the screen there is a glitch in the audio, this happens also when you press the volume up / down buttons on the phone itself. After days of debugging…
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
0
votes
1 answer

Circular Array Queue

Im trying to implement a circular array queue without a count variable. Im looking for a way to determine if the array is full, for it to call the expand method but it doesn't seem to be working any idea why? Thanks! public void enqueue (T element)…
JProg
  • 189
  • 1
  • 9
  • 15
0
votes
2 answers

NullPointerException being thrown and program doesn't terminate as expected

I am creating a stock exchange type program and so far I've got the input working so that it takes the command from the user properly. However what it does to the input isn't working as expected. The first thing I am confused on is why it is…
azdiiv
  • 383
  • 5
  • 17
0
votes
1 answer

CoreAudio does not fill buffer running on device (but in simulator)

I am building a CoreAudio application, where i need to process raw input samples from the microphone. I use two threads, where one thread is producing samples from the microphone input and the other thread is consuming samples. These threads share a…
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
0
votes
0 answers

Reading chunks from an SQlite blob when a chunk might span two blobs

I have a situation where I need to read arbitrarily-sized (but generally small) chunks of binary data from an SQlite database. The database lives on disk, and the data is stored in rows consisting of an id and a read-only blob of between 256 to 64k…
ChrisM
  • 2,128
  • 1
  • 23
  • 41
0
votes
1 answer

Circular buffer of an array?

I have a two dimensional double I need to keep in a circular buffer. I'd prefer to keep it together in one buffer instead of having to keep up with two single dimensional buffers. Is it possible to do this easily, or would it be best to go ahead…
Johnny
  • 5
  • 2
0
votes
1 answer

How to insert strings into buffer?

I'm new to C programming. I want to create circular buffer which writes strings into text file. #include #include // Buffer writer header files #include typedef struct { char value; } ElemType; /* Circular buffer…
user1285928
  • 1,328
  • 29
  • 98
  • 147
0
votes
0 answers

ring like data structure in C++

Possible Duplicate: Does a standard implementation of a Circular List exist for C++? I'm looking for a data structure where the access behaves "ring-like", so if I increment the iterator and the end is reached, it is reset to the beginning. If I…
wal-o-mat
  • 7,158
  • 7
  • 32
  • 41
0
votes
1 answer

Producer/consumer seems to be in deadlock when buffer is smaller than input from producer

I made a circular buffer with multiple clients writing (in the end I want them to write messages of different size) into a buffer. The server reads them out. It's based on the code in a consumer/producer problem: #include #include…
Thomas
  • 1,678
  • 4
  • 24
  • 47
0
votes
1 answer

threaded buffer: how to prevent racing between clients and have client and server working on buffer simultaneously

I made a program that has a few threaded clients that can store information into a circular buffer; the server can read (and 'remove') the messages from the circular buffer. The problem I have is when the buffer is full; the client that is currently…
Thomas
  • 1,678
  • 4
  • 24
  • 47
0
votes
0 answers

In .NET, how to create a stream that has fastest possible Write() method

I have an application that logs messages to two files in conjuction with sending them over a socket. One file gets the binary representation and the other gets a human-readable equivalent. The message class has a function that outputs the…
sevzas
  • 701
  • 2
  • 5
  • 13
0
votes
2 answers

Circular buffers (start value gets removed, what happens?)

I'm trying to get my head around Circular/ring buffers and I'm a little stuck on a question. If I have a linear queue such as the following: 66,    20,      30,      40,      19,      empty slot 0         1         2         3         4 …
user676853
0
votes
1 answer

How to account for a circular buffer size of 1 when using pthreads / cond_signal / cond_wait?

I'm trying to implement a program that uses a thread to read data from a file and write to a buffer of an arbitrary size, while two other threads read info from this buffer. Everything works fine, except for when I specify a buffer size of 1. When I…
thomascirca
  • 893
  • 4
  • 14
  • 30
1 2 3
27
28