initium1
asked on
what circ buffer?
what circ buffer?
simple source..
plz...
simple source..
plz...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
circ buffer = queue first in first out where you put the data in an array that is managed in circular fashion.
Ah lets take look at the example.
buffer [data1][data2][ ][ ][ ] MAX_BUFFER
tail head
You put the data in the head.
Each time the data is put, the head is increased. After the head reached the MAX_BUFFER, the head is rolled back to ZERO ===> thats why they call it circular. If the head reached the tail again (from the back), thats mean the buffer is full.
On the other side, you get the data from the tail and increased the tail. If the tail == to the head, thats mean the buffer is empty.
Ahhh .. look at the code and learn bys.
#define MAX_BUFFER 10;
struct {
int head, tail;
buffer[MAX_BUFFER];
} CB;
// On empty state, head == tail
int Init(CB* cb)
{
cb->head = cb->tail = 0;
}
int Put(CB* cb, int data)
{
int new_head = cb->head+1;
if (new_head >= MAX_BUFFER) new_head = 0;
if (new_head == cb->tail) return 0; // FULL
cb->buffer[cb->head] = data;
cb->head = new_head;
}
int Get(CB* cb, int data)
{
int old_tail = cb->tail;
if (old_tail == cb->head) return 0; // EMPTY
cb->tail = old_tail + 1;
if (cb->tail >= MAX_BUFFER) then cb->tail = 0;
return cb->buffer[old_tail];
}
main()
{
CB cb;
Init(&cb);
Put(&cb, 1);
Put(&cb, 2);
Put(&cb, 3);
printf("%d ", Get(&cb));
printf("%d ", Get(&cb));
printf("%d ", Get(&cb));
}
Ah lets take look at the example.
buffer [data1][data2][ ][ ][ ] MAX_BUFFER
tail head
You put the data in the head.
Each time the data is put, the head is increased. After the head reached the MAX_BUFFER, the head is rolled back to ZERO ===> thats why they call it circular. If the head reached the tail again (from the back), thats mean the buffer is full.
On the other side, you get the data from the tail and increased the tail. If the tail == to the head, thats mean the buffer is empty.
Ahhh .. look at the code and learn bys.
#define MAX_BUFFER 10;
struct {
int head, tail;
buffer[MAX_BUFFER];
} CB;
// On empty state, head == tail
int Init(CB* cb)
{
cb->head = cb->tail = 0;
}
int Put(CB* cb, int data)
{
int new_head = cb->head+1;
if (new_head >= MAX_BUFFER) new_head = 0;
if (new_head == cb->tail) return 0; // FULL
cb->buffer[cb->head] = data;
cb->head = new_head;
}
int Get(CB* cb, int data)
{
int old_tail = cb->tail;
if (old_tail == cb->head) return 0; // EMPTY
cb->tail = old_tail + 1;
if (cb->tail >= MAX_BUFFER) then cb->tail = 0;
return cb->buffer[old_tail];
}
main()
{
CB cb;
Init(&cb);
Put(&cb, 1);
Put(&cb, 2);
Put(&cb, 3);
printf("%d ", Get(&cb));
printf("%d ", Get(&cb));
printf("%d ", Get(&cb));
}
Nothing has happened on this question in more than 10 months. It's time for cleanup!
My recommendation, which I will post in the Cleanup topic area, is to
accept answer by akshayxx (good answer by Kocil as well).
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jmcg
EE Cleanup Volunteer
My recommendation, which I will post in the Cleanup topic area, is to
accept answer by akshayxx (good answer by Kocil as well).
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jmcg
EE Cleanup Volunteer
taking a simple example
char a[5];
if u push "hello" to this
then it will fill up the array..
and if u push further data .. say "zz"
then array will look like
"zzllo"
i dont think if any standard implementation gives u circular buffer .. u'll have to implement on ur own .. or use someone else implementation