Reputation: 24067
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?
Upvotes: 1
Views: 764
Reputation: 16353
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.
Upvotes: 2
Reputation: 96241
If your objects are sufficiently expensive to instantiate, you could preallocate a pool of them and then put shared_ptr
s to the pooled objects into the circular buffer to reuse them.
Upvotes: 3