简体   繁体   中英

Circular Buffer in Qt

What is the best way to create a circular buffer in Qt? I would just do something like this:

vidoudpsocket.h

typedef struct
{
    CircularBuffer *before
    quint16 *data;
    CircularBuffer *next;
} CircularBuffer;

videoudpsocket.cpp

VideoUDPSocket::VideoUDPSocket(QObject *parent)
    : QObject(parent)
{
    CircularBuffer buffer0, buffer1, buffer2, buffer3, buffer4, buffer5, buffer6, buffer7;
    buffer0.before = buffer7;
    buffer0.data = (quint16 *) malloc(16384*16384);
    buffer0.next = buffer1;
    //...
}

Is this a good way to implement it in Qt or is there a better?

Thank you

Edit:

My first try do not even seems to work, the compiler does not know what to do with CircularBuffer inside the structure.

I now try to use QVector but i alwas get the Error allocating memory for data[i] -Debug-Message (at i > 7809).

#define MAXNUMBERRANGEBINS 8192
QVector<quint8**> ringBuffer;
ringBuffer.resize(8);
foreach(quint8** data, ringBuffer)
{
    data = (quint8**) malloc(MAXNUMBERRANGEBINS*2*sizeof( quint8* ));
    if(data == NULL)
        qDebug() << "Error allocating memory for data";
    for(int i = 0; i < MAXNUMBERRANGEBINS*2; i++)
    {
        data[i] = (quint8*) malloc(MAXNUMBERRANGEBINS*2);
        if(data[i] == NULL)
            qDebug() << "Error allocating memory for data[" << i << "]";
    }
}

Edit2 If I calculated right, my array is about 270MB big per buffer, that should explain the memory allocation error, am I right?

Don't use selfmade linked lists. Direct access is easier to understand and easier to debug.

     QVector<uint16_t*>(8) ringBuffer;
     foreach(uint16_t* data, ringBuffer){
       data = new(1024);
     }

     size_t index = 0;

     //Access 
     while(true)
     {

       memcpy(ringBuffer[index],yourSource, 1024);
       index = (index +1) % 8;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM