Click here to Skip to main content
15,036,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I have a need for a ring buffer (In C language)

What I have tried:

Hi everybody,

I have a need for a ring buffer (In C language) which can hold objects of any type at the run time (almost the data will be different signal's values like current (100ms and 10ms) and temperature.etc) ( I am not sure if it have to be a fixed size or not) and it needs to be very high performance. although it's in a multi-tasking embedded environment.

Actually i need this buffer as a back up, which mean the embedded software will work as normal and save the data into the ring buffer, so far for any reason and when an error occurred, then i could have like a reference for the measured values then i will be able to have a look on them and determine the problem. Also i need to make a time stamp on the ring buffer, which mean every data (Signal value) is stored on the ring buffer will stored with the measurement's time.

Any code or ideas would be greatly appreciated. some of the operations required are:

create a ring buffer with specific size. Link it with the whole software. put at the tail. get from the head. at error, read the data and when its happen (time stamp). return the count. overwrite when the buffer is being full.
Updated 17-Aug-18 2:31am
Rick York 16-Aug-18 12:55pm
Have you tried searching for the phrase "ring buffer" both here and at google? There are lots and lots of results found, and a fair number here too.

It doesn't quite work like that.
We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to and ask there.

But be aware: you get what you pay for. Pay peanuts, get monkeys.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements."[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
So either pay someone to do it, or learn how to write it yourself. We aren't here to do it for you.
I have a need for a ring buffer (In C language) which can hold objects of any type at the run time

It may be difficult since C is strongly typed and is not object.
You need to refine you requirement.
If I understand, you want a buffer to log last n entries to be able to reread entries after error.
Circular buffer - Wikipedia[^]

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.
Create a structure to be stored in the ring buffer:
typedef struct rbuf_data 
    time_t timestamp; /* or any other time type */
    int data_type; /* defines the type of data (and so also it's length) */
    void *data; /* with variable data size */
    /*char data[MAX_DATA_SIZE];*/ /* with fixed or limited data size */
rbuf_data rbuf[RBUF_SIZE];

When having data with variable size, the data must be allocated using malloc() before passing to the put function and freed when overwriting upon buffer overuns.

Otherwise copy the data using memcpy() with the corresponding size when adding new data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900