Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:29:04+00:00 2026-06-17T22:29:04+00:00

Short story I need to add incoming messages data to a fixed length buffer.

  • 0

Short story

I need to add incoming messages data to a fixed length buffer.
If buffer is full, replace oldest message data (or oldest bytes in buffer) with new one. This is some kind of a queue.
How to implement this kind of queue?
Program is in C and I have little (more like none) knowledge of C.

Long Story

I have a program that is receiving messages from a device.
This messages might be of variable length and have an associated Id.
Currently the program only keeps the latest message received for each Id.
This messages are kept internally by the program and are only displayed to the user if there’s a configuration for it, or if he sends a command (in real time) to get the received message for a given Id.
Now there’s a requirement to keep the several received messages for a given Id (not just the last) and return them.
They don’t need to be returned separately. They can be returned all in one block (think in the several messages as a piece of memory data that the device is returning).
Currently there’s a fixed length buffer to keep the received message and a variable to keep track of the received message length.
So I was thinking of using the existing buffer and to just keep adding new messages to it.
And If another message comes and there’s not more space left on the buffer, then I should overwrite the oldest message data. Something like a queue.
Especially this last part of implementing this kind of a queue is where I’m needing more help.
The program is in C, was not done by me, and my knowledge of C is limited (not to say non existing 🙂
I really should be learning C first, but there’s no time for that now 🙂
I’ve done C#, Java, Javascript and mostly.

Any help on managing this king of queue?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:29:05+00:00Added an answer on June 17, 2026 at 10:29 pm

    A circular buffer would help in this case, with some slight modification that allows you to kick out the oldest entry when the buffer runs full. Imagine you a have some typedef struct msg {...} msg and an array message[N] of this kind, then you manage two indices, int in, out that are both initialized to 0.

    While out != in you may read messages from this array. You implement the incrementing of both in and out as wrapping around, using some form of out = (++ out) % N;. So far this is not different than any other circular buffer.

    Now, let’s address your question of replacing the oldest message in case the buffer is full, i.e. when (in + 1) % N == out at the time when you are about to perform an insertion. With a circular buffer, this now is quite straightforward. To make space, out is pushed forward by one, allowing you to increment in anyways, then you insert:

    if (((in + 1) % N) == out)
    { 
        out = (++ out) % N;
    }
    in = (++ in) % N;
    message[in] = incoming_msg;
    

    Pushing the reader’s index out forward, allows this position to be used for the most recent message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To make a long story short, I need to use JavaScript to add an
Long story short, client's hosting is using php 5.2.5 and i desperately need to
So long story short, i have some forms (2) of them that, need different
I've got a project where we'll need to generate a lot of fixed-length random
Long story short: I need the inverted function of WeekdayName() , in the following
Long story short is. I created this http://jsfiddle.net/paDmg/368/ and I would like to add
Long story short, I'm trying to store corresponding data values from a JSON returning
To cut a long story short I am attempting to add some functionality to
Long story short, I need to update a custom field in my standard Contact,
Short story: stopPropagation() prevents a dropdown menu from closing - which is good. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.