Click here to Skip to main content
Click here to Skip to main content

A circular character buffer

, 9 Dec 1999
Rate this:
Please Sign up or sign in to vote.
A circular, thread-safe read/write character buffer
  • Download demo project - 11 Kb
  • <!-- Article Starts -->

    I recently needed a class that stored a character array as a circular buffer. If you are unfamiliar with circular buffer, it is a permanently allocated buffer that has both a read and write position. I have done a few of them in the past, but had lost all of my examples. So I wrote a new one and placed it into a class called CircularBuffer. The CircularBuffer can be read from and written to, and it has methods to get the read and write count. As the buffer is written to the write position increases til the end of the buffer is reached, at that point the write position is wrapped back to the start of the buffer and writing starts from there. The same happens to the read operation also, but the read position is incremented. It is important to note that the read position will never be greater than the write position, since if it were we would be reading data that had not been written to the buffer. Likewise, the write position will never wrap the read position otherwise data that had not been read would be overwritten.

    The basic operations are read and write, both fail if an overlap condition could occur.

    To get the number of availible characters to read the readCount method is used. If the count is greater than zero use the read method to get the latest contents written into the buffer.

    I have included a small test application in a project TestQue. The test driver is in the TestQue.cpp. It is multithreaded win32 console app developed using VC6.

    The CircularBuffer code is in CicularBuffer.cpp and h.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    David Hubbard

    United States United States
    No Biography provided

    Comments and Discussions

    GeneralA circular buffer that can grow or decrease PinmemberUDria4-Dec-01 1:01 
    GeneralRe: A circular buffer that can grow or decrease PinsussMoshe Beeri22-May-05 3:45 
    here it is for c & C++ programmers
    UDria, it is a magnificent algorithm. 10x
    long gcd (long x, long y)
    long temp;
    long g;
    if (x < y) { temp = x; x = y; y = temp; }
    g = 0;
    while ((0 == (x & 1)) && (0 == (y & 1))) { /* While both even */
    x >>= 1; y >>= 1; g++;
    while (x != 0) {
    while (0 == (x & 1)) { x >>= 1; }
    while (0 == (y & 1)) { y >>= 1; }
    temp = (x-y)/2; if (temp<0) { temp = -temp; }
    if (x >= y) {
    x = temp;
    } else {
    y = temp;
    return y<<g;
    unsigned char buffer[1024];

    void FlatenCircular(
    char *buffer,
    int hd, //the index of head
    int sz, //size of buffer
    int lt //number of used elements of buffer
    long l_gcd = gcd(sz, hd); //gcd, number of external for cycles
    int stp = sz / l_gcd; //number of internal for cycles
    int sum = sz - hd; //
    int rd, swp; // read, swap
    for (int j=0; j < l_gcd; j++)
    int y = (hd + j) % sz;
    char rd = buffer[y];
    for (int i=0; i < stp;i++)
    y = (y + sum) % sz;
    swp = buffer[y];
    buffer[y] = rd;
    rd = swp;
    void main(){
    long cd = 1171;
    long l1 = 1103*1109*cd;
    long l2 = 1129*1151*cd;
    printf("%d\n",gcd( l1,l2));
    unsigned char d=0;

    for(int i=500; i<1524; i++ )
    buffer[i%1024] = d++;

    FlatenCircular( (char*)&buffer[0], 500,1024, 1024);
    for(i=0; i<1024; i++ )
    if( buffer[i] != d++ )
    printf("Error At index %d\n",i );

    QuestionCircular Buffer code... another little error in code? PinsussBruno Gallichand15-Sep-00 9:24 
    AnswerRe: Circular Buffer code... another little error in code? PinsussBruno Gallichand15-Sep-00 9:31 
    AnswerRe: Circular Buffer code... another little error in code? PinmemberUnsigned Char20-Dec-00 8:28 
    Generalbugs Pinsussfred huang6-Sep-00 12:54 
    GeneralRe: bugs Pinmemberjm.alkema2-Feb-06 4:09 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.150327.1 | Last Updated 10 Dec 1999
    Article Copyright 1999 by David Hubbard
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid