I was considering to use boost circular_buffer. From here :
In order to achieve maximum efficiency, the circular_buffer stores its elements in a contiguous region of memory, which then enables:
Use of fixed memory and no implicit or unexpected memory allocation.
But I supposed to use circular_buffer calling cb.push_back(myObj)
. To make this call I have to instatiate this object. When I call push_back
my new object replace the old, outdated object from circular_buffer.
So while using circular_buffer I do intensive allocate memory, because I have to instatiate new object for every push_back
.
But I really want to avoid new objects instatiation. Instead I want to "reconfigure" objects from circular_buffer
. If it is possible? Can you suggest circular-buffer which allows to reuse objects inside it so avoiding expensive runtime memory allocations?
如果您的对象足够昂贵,无法实例化,则可以预分配它们的池,然后将shared_ptr
与池对象放到循环缓冲区中以重用它们。
Perhaps you want a circular iterator rather than a circular buffer . Fill a fixed length buffer with default constructed elements. Then as you fill elements (through the circular iterator) you can just modify the structures instead of copying over.
For an example of a circular iterator, check the accepted answer to this question .
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.