?
Solved

what circ buffer?

Posted on 2003-03-06
5
Medium Priority
?
262 Views
Last Modified: 2012-05-04
what circ buffer?
simple source..
plz...
0
Comment
Question by:initium1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8086014
circular buffer means if u r at the end of the buffer, and u want to insert more data in the buffer then it will go to the start of the buffer

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
0
 
LVL 8

Accepted Solution

by:
akshayxx earned 80 total points
ID: 8086021
http://www.cs.utk.edu/~calloway/prg/cq/
simple circular implementation for you
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8086090
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));
}
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10089357
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question