This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Problem with circular buffer



Hello guys,

I have a little problem when implementing a circular Buffer.

I've defined two structs:

typedef

struct{

short value;

}ElemType;

typedef

struct{

long size;

long start;

long end;

ElemType *elems;

}Circular_Buffer;

After that in a loop in main program i read the value contained in a register, and I want to record it into the Buffer:

data->value = I2S2_RXLT1;

Write_Circular_Buffer(&Buffer,data);

I have declared the structs as follows:

ElemType *data;

Circular_Buffer Buffer;

The Write_Circular_Buffer function is the next:

void

Write_Circular_Buffer(Circular_Buffer *cb, ElemType *elem)

{

cb->elems[cb->end] = *elem;

cb->end = (cb->end + 1) % cb->size;

if (cb->end == cb->start)

{

cb->start = (cb->start + 1) % cb->size;

}

}

The problem is that after that If i look at the value in Buffer.elems[i].value, it doesn't correspond with the values of data->value.

Hope you can help me,

Best regards,

Albert Torné

  • Hello,

    I haven't examined the whole program, but I see a problem with ElemType *data;

    data is just a pointer and doesn't point to anything.  You need to create an ElemType, i.e. ElemType dataItem and then have data point to it, e.g.

    ElemType dataItem;

    ElemType * data;

    data = &dataItem;

    Stephen

     

  • Hello! Thanks for your answer.

    In fact I've changed the declaration of data:

    "ElemType data;"

    However, I think that it is not working yet.

  • Are you setting all the Buffer parameters? i.e.

       ElemType dataArray[5];

       Buffer.size = 5;

       Buffer.elems = dataArray;

       Buffer.start = 0;

       Buffer.end = 0;

    Also,  you shouldn't update your start pointer in the write function.  That should be done in the read function. 

    Stephen

  • I have initialized the buffer before by setting this:

    void

    Init_Circular_Buffer(Circular_Buffer *cb, longsize)

    {

    cb->size=size;

    cb->start = 0;

    cb->end = 0;

    cb->elems = (ElemType*)calloc(cb->size,sizeof(ElementType));

    }

    calling this function

    void Init_Circular_Buffer(&Buffer);

  • You shouldn't put void before calling the function.  Also, the function call is missing the size parameter.

    Otherwise, I don't see anything else wrong with it.  I suggest stepping through the program to see what's happening at each point.

    Stephen

  • Yes, Sorry I call the function Init_Circular_Buffer(%buffer, size); (not any void. typing error)

    I step through the program and I can see that the value of the register that i'm saving into the buffer is different from the one that i read from the buffer. And of course, they would be equal.

  • The following attached file seems to work:

    typedef struct
    {
    
       short value;
    
    } ElemType;
    
    typedef struct
    {
    
       long size;
    
       long start;
    
       long end;
    
       ElemType *elems;
    
    } Circular_Buffer;
    
    Circular_Buffer buffer;
    
    void Write_Circular_Buffer(Circular_Buffer *cb, ElemType *elem);
    
    void main(void)
    {
       ElemType data[10];
       ;
       ElemType dataArray[5];
       int i;
    
       Circular_Buffer Buffer;
    
       Buffer.size = 5;
       Buffer.elems = dataArray;
       Buffer.start = 0;
       Buffer.end = 0;
    
       for (i = 0; i < 10; i++)
       {
          data[i].value = i;
       }
    
       Write_Circular_Buffer(&Buffer, &data[0]);
       Write_Circular_Buffer(&Buffer, &data[1]);
       Write_Circular_Buffer(&Buffer, &data[2]);
       Write_Circular_Buffer(&Buffer, &data[3]);
       Write_Circular_Buffer(&Buffer, &data[4]);
       Write_Circular_Buffer(&Buffer, &data[5]);
       Write_Circular_Buffer(&Buffer, &data[6]);
       Write_Circular_Buffer(&Buffer, &data[7]);
    
    }
    
    void Write_Circular_Buffer(Circular_Buffer *cb, ElemType *elem)
    {
    
       cb->elems[cb->end] = *elem;
    
       cb->end = (cb->end + 1) % cb->size;
    
       if (cb->end == cb->start)
       {
    
          cb->start = (cb->start + 1) % cb->size;
    
       }
    
    }
    
    


    I would recommend moving the following to a separate Read_Circular_Buffer() function.

    if (cb->end == cb->start)   

    {

          cb->start = (cb->start + 1) % cb->size;

    }

  • Thank you! I will test it.

    I have put the following into the write function for one reason:

    if (cb->end == cb->start)   

    {

          cb->start = (cb->start + 1) % cb->size;

    }

     

    I want to write every time in the buffer, and past a time print all the elements in the buffer. I am not doing write and read each time. I want to write and when the buffer finishes, follow in the beginning overwriting the existing values.

    And at a certain time, I will print the elemens contained in the buffer in that moment.

    Thank you