How do I code a simple integer circular buffer in C/C++?

后端 未结 4 1284
长发绾君心
长发绾君心 2021-02-05 08:54

I see a lot of templates and complicated data structures for implementing a circular buffer.

How do I code a simple integer circular buffer for 5 numbers?

I\

4条回答
  •  灰色年华
    2021-02-05 09:29

    If the size and data type of your buffer are fixed, a simple array is all you need:

     int buffer[5];
    

    Add to that a couple pointers:

     int* start = &buffer[0];
     int* end   = &buffer[4]+1;
     int* input = start;
     int* output = start;
    

提交回复
热议问题