Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>(Edit: sorry, misread your question; previous answer removed.)</p> <p>You can use any memory pool for your objects. Usually you group together same (or similar) sized objects in the same pool. Since you usually have to call a special <code>delete</code> function on the pool I suggest you use a <code>shared_ptr</code> with a custom deleter. You can then use this <code>shared_ptr</code> with any standard container you like.</p> <p>Edit: It seems like an example is needed. Warning: this is totally untested and from the top of my head. Don't expect it to compile.</p> <pre><code>#include &lt;boost/pool.hpp&gt; #include &lt;memory&gt; class A; class B; // B inherits from A int main() { // could be global boost::object_pool&lt;A&gt; a_pool; boost::object_pool&lt;B&gt; b_pool; std::vector&lt;std::shared_ptr&lt;A&gt;&gt; v; v.push_back(std::shared_ptr&lt;A&gt;(a_pool.construct(), [&amp;](A*p) { a_pool.free(p); })); v.push_back(std::shared_ptr&lt;A&gt;(a_pool.construct(), [&amp;](A*p) { a_pool.free(p); })); v.push_back(std::shared_ptr&lt;A&gt;(a_pool.construct(), [&amp;](A*p) { a_pool.free(p); })); v[2] = std::shared_ptr&lt;B&gt;(b_pool.construct(), [&amp;](B*p) { b_pool.free(p); }); return 0; } </code></pre> <p>This will work even is B is much larger than A. It also does not rely on the automatic freeing of the pool which is IMHO dangerous. The memory layout is not tight because the pool will always overallocate, but it will have no fragmentation and this is what you want if I understood your question.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload