简体   繁体   English

Qt 中的循环缓冲器

[英]Circular Buffer in Qt

What is the best way to create a circular buffer in Qt?在 Qt 中创建循环缓冲区的最佳方法是什么? I would just do something like this:我会做这样的事情:

vidoudpsocket.h vidoudpsocket.h

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

videoudpsocket.cpp视频udpsocket.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?这是在 Qt 中实现它的好方法还是有更好的方法?

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.我的第一次尝试似乎不起作用,编译器不知道如何处理结构内部的CircularBuffer

I now try to use QVector but i alwas get the Error allocating memory for data[i] -Debug-Message (at i > 7809).我现在尝试使用 QVector,但我总是收到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? Edit2如果我计算正确,我的数组每个缓冲区大约 270MB,这应该可以解释 memory 分配错误,对吗?

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;
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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