Note that there are some explanatory texts on larger screens.

plurals
  1. POPreventing Memory Fragmentation in Polymorphic Container
    text
    copied!<p>This question requires some explaning, so thumbs up if you don't skip the example :)</p> <p>I recently read a book describing memory fragmentation (on the heap) in some detail and it got me thinking about my own projects. For example, when using a ptr_container (from Boost) in the following way</p> <pre><code>ptr_array&lt;A&gt; arr; // arr.push_back(new A()); // a1. arr.push_back(new A()); // a2. arr.push_back(new A()); // a3. .... </code></pre> <p>it will quite fast lead to some memory fragmentation when replacing elements. For the sake of argument, lets say that the actual container can hold all pointers we give to it. The heap will look something like:</p> <pre><code>[arr_array][a1][a2][a3]...[aN] </code></pre> <p>When we start to replace pointers with a subtype (that has a larger size) this situation changes, lets say we replace all objects referenced by odd pointers (a1, a3, ...) to a larger type, then it'll look like:</p> <pre><code>[arr_array][xx][a2][xx][a4]...[aN][b1][b3]...[bN] </code></pre> <p>where [xx] denotes unused space and b1...bN are the new objects.</p> <p>So what I'd like is a container that stores the objects (like in STL-containers) but supports polymorphic storage. I do know how to implement this kind of container but I prefer to use "expert" libraries (Boost, STL, ...). To sum it up, my question:</p> <p><b>Is there a container that supports (dynamically allocated) polymorphic objects saved in a continuous sequence of memory?</b></p> <p>For example, the memory layout could look like this:</p> <pre><code>[arr_array] = [ptr_lookup_table][storage] = [p1, p2, ..., pn][b1, a2, b3, ..., aN] </code></pre> <p>Thank you for your answers / comments!</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