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 6968457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:27:15+00:00 2026-05-27T16:27:15+00:00

I have coded a simple ring buffer which has a ring size of 5

  • 0

I have coded a simple ring buffer which has a ring size of 5 to store values of type A.
Now I have to extend this buffer to store type B values(also 5 values).

To give an overview, I have defined the variables for read index and write index as global volatile and two functions for reading and writing on the ring buffer.

I only have to do : ring data = int read_ring_data() and write_ring_data(int pass_new_data)

The volatile global variables help control the locations of read and write.

My question is, is there a way to reuse these read and write functions for extending it to a 2D buffer by simply re-dimensioning it? How do I implement it?

  • 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-05-27T16:27:16+00:00Added an answer on May 27, 2026 at 4:27 pm

    You can still code in an object oriented style in C , simply using struct’s as classes, and ‘methods’ are just functions that take a pointer to a class. I would create a general purpose ‘ring-buffer’ ‘class’ in C as follows..

    typedef struct RingBuffer {
      int elemSize;
      int headIndex; // index to write
      int tailIndex; // index to read
      int maxIndex;
      void* buffer; 
    }
    RingBuffer;
    // initialize a new ring-buffer object
    void RingBuffer_Init(RingBuffer* rb, int elemSize, int maxNum)  {
      rb->elemSize=elemSize; rb->headIndex = 0; rb->tailIndex=0; rb->buffer = malloc(elemSize*maxNum);
      rb->maxIndex=maxNum;
    }
    void RingBuffer_Read(RingBuffer* rb,  void* dstItem){ // copy into buffer, update index
       void* src=rb->buffer + rb->tailIndex*rb->elemSize;
       memcpy(dstItem,src,rb->elemSize);
       rb->tailIndex++; ....//wrapround, assert etc..
    }
    void RingBuffer_Write(RingBuffer* rb, const void * srcItem) {  // copy from buffer,update indices
    }// etc..
    

    you’d have to take care of allocating the RingBuffer structs of course, some people might do that with some macro if they adopt a consistent naming scheme for ‘init'(equiv of c++ constructor) and ‘shutdown’/’release’ functions

    Of course there are many permutations.. it would be very easy to make a ring buffer into which you can read/write variable sized elements, perhaps writing the element size into the buffer at each point. it would certainly be possible to resize on the fly aswell, even change the element size.

    Although the language support for creating data structures is more primitive in C than in C++, sometimes re-working a problem to use simple data structures can have performance benefits. Also treating data structures as simple blocks of memory with size passed as a parameter may cause less compiler inlining: compact code can have advantages as the default method to use outside of inner loops (i-cache coherency).

    it would be possible to combine the ‘Buffer Header’ structure and the array data into one allocation, (just assume the buffer data follows the header structure in memory), which reduces the amount of pointer-dereferencing going on.

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

Sidebar

Related Questions

I have coded a simple PHP Countdown Timer using this code: list($date,$time)=explode( ,$compounddate); list($thour,$tminute,$tsecond)=explode(:,$time);
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have a simple code byte[] buffer = Encoding.UTF8.GetBytes(abracadabra); MemoryStream ms = new MemoryStream();
There is probably is simple fix for this but I currently have code similar
I am a total beginner in JS/XML. I have this simple code that needs
I have coded a simple div containing an image that follows the mouse cursor
I coded a simple form to calculate a price by size, shape, color and
i have an android application which has a listview for listing 14 items inside
I have a simple question; hope you can help me. I have coded a
I am using the Google Checkout API to code a simple store. I have

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.