Skip to content

abreheret/circular_buffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Circular Buffer

Simple implemetation of circular buffer in c

Example :

#include "circular_buffer.h"
#include <stdio.h>

typedef struct {
	int value;
	float fvalue;
}ElemType; 

int main(int agrv,char **argv) {
    CircularBuffer_t cb;
    int size_max = 8; 
    cb_init(&cb, size_max,sizeof(ElemType));
 
    // Fill buffer with elements
    for (int i = 0 ; i < 25 ; i ++) {
		ElemType elem;
		elem.value = i;
		elem.fvalue = i*3.14159265359;
		printf("push element {%02d %4.2f} size before push : [%d] \n", elem.value,elem.fvalue,cb.size);
        cb_push(&cb, &elem);												  
	}																		  

	// Read elements 
	for(int i = 0 ; i < cb.size ; i++) {									  
		ElemType * e = (ElemType *)cb_read(&cb,i);						  
		printf("read element {%02d %4.2f} size : [%d] \n", e->value,e->fvalue,cb.size);
	}																		  

    // Pop, Remove and print all elements 									  
    while (!cb_is_empty(&cb)) {												  
		ElemType elem;														  
		cb_pop(&cb,&elem);													  
		printf("pop element  {%02d %4.2f} size after pop : [%d] \n" , elem.value,elem.fvalue,cb.size);
    }
	
    cb_dead(&cb);
    return 0;
}

About

simple implemetation of circular buffer in c

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published