Circular buffer simulation in C

Okay, time for a programming post. I’m going to share a small C program I wrote a while ago. It’s an interactive program that simulates a circular buffer. A circular buffer is a data structure where data elements are inserted at one end and taken off the other end. It’s sort of like a queue, but it’s more compact and a limited size.

EDIT: Forgot to change the angle brackets to < and > in the HTML code. It’s fixed now.

 1 /**********************************
 2  * circular-buffer.c - simulation *
 3  * of a circular buffer           *
 4  * Author: ZenHacker2015          *
 5  * Date: Started Jan 15, 2015     *
 6  *       Contd   Jan 16, 2015     *
 7  **********************************/
 8 
 9 #include <stdio.h>
10 #include <ctype.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 
14 int consume( void );
15 void produce( int );
16 void printbuf( void );
17 void increment( int ** );
18 
19 int *buffer;    // array used as the circular buffer
20 int *start;     // pointer to the start of the data
21 int *end;       // pointer to the end of the data
22 int bufsize;    // size of the buffer, as given by command line argument
23 
24 int main( int argc, char **argv ){
25         // Initialization:
26         bufsize = atoi( argv[1] );
27         buffer = (int *) malloc( sizeofint ) * bufsize );
28         start = end = &buffer[0];
29         char readbuffer[20];
30 
31         puts( "CIRCULAR BUFFER SIMULATOR" );
32         puts( "Commands:" );
33         puts( "consume - Consume an item from the buffer" );
34         puts( "print - Print the contents of the buffer" );
35         puts( "<number> - Add item with value <number> to the buffer" );
36         puts( "Type Ctrl+C to exit." );
37 
38         // Loop and get input from user:
39         for(;;){
40                 printf( "> " );   // prompt
41                 fflush( stdout ); // print prompt immediately
42                 read( 0, readbuffer, 20 );
43                 if( readbuffer[0] == 'c' )
44                         printf( "%d\n", consume() );
45                 else if( readbuffer[0] == 'p' )
46                         printbuf();
47                 else if( isnumber( readbuffer[0] ) )
48                         produce( atoi( readbuffer ) );
49                 else
50                         printf( "Invalid command.\n" );
51         }
52 
53         return 0;
54 }
55 
56 // Consumes an item from the buffer
57 int consume(){
58         int val = *start;
59         increment( &start );
60         return val;
61 }
62 
63 // Places an item into the buffer
64 void produce( int item ){
65         *end = item;
66         increment( &end );
67 }
68 
69 // Prints the contents of the buffer, fromt start to end
70 void printbuf(){
71         if( start < end ){
72                 forint *p = start; p < end; p++ ){
73                         printf( "%d ", *p );
74                 }
75         }
76         else{
77         // Note: If start == end, this can be interpreted as either
78         // a full buffer or an empty buffer.
79                 forint *p = start; p < buffer + bufsize; p++ ){
80                         printf( "%d ", *p );
81                 }
82                 forint *p = buffer; p < end; p++ ){
83                         printf( "%d ", *p );
84                 }
85         }
86         printf( "\n" );
87 }
88 
89 // Increments start/end when an item is consumed/produced
90 void increment( int **pointer ){
91         if( *pointer - buffer < bufsize - 1 )
92                 (*pointer)++;
93         else
94                 *pointer = buffer;
95 }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s