Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Say I have an object that contains three stl vectors. Is my thinking correct that if they are regular members of the class, the memory of them will be "together" for the whole object? e.g. my memory would look sth like 10 blocks vector A, 5 blocks vector B, and then 15 blocks vector C.</p> </blockquote> <p>Each <code>vector</code> occupies a fixed size in the containing object, independent of the number of elements currently stored by the <code>vector</code>. It is likely that the <code>value_type</code> of the <code>vector</code> (e.g. <code>vector&lt;int&gt;</code> has <code>value_type</code> <code>int</code>) won't affect the size of the contained <code>vector</code> object itself: only the amount of heap-allocated store the <code>vector</code> needs in order to maintain its storage capacity (so they're likey to be say 8 or 16 or 32 bytes each but all the same, but not 10 "blocks" (whatever that might be), 5 blocks and 15).</p> <blockquote> <p>Would then once I insert more objects into vector A so that the space runs out the whole structure including vector B and C need to be moved?</p> </blockquote> <p>Inserting elements into <code>A</code> can only ever cause existing elements in <code>A</code> to be moved (when the capacity is exceeded). <code>B</code> and <code>C</code> can never be affected.</p> <blockquote> <p>Is that then an argument for pointers? Or are vectors internally only a pointer to the allocated memory?</p> </blockquote> <p>YES, it's an argument... such a good one that YES, <code>vector</code>s already <em>do</em> use pointers to the contiguous memory where the actual <code>value_type</code> elements are stored.</p> <blockquote> <p>Same question would go for lists etc...</p> </blockquote> <p>YES, <code>list</code>s store their <code>value_type</code> elements on the heap too, and the size of the object embedding or derived from <code>list</code> is unaffected by operations on the <code>list</code>.</p> <blockquote> <p>Are there any rules of thumb as to the cost of a redirection vs the cost of copying small objects? Maybe sth along the lines of 5 pointer redirection = 1 integer copy?<code>enter code here</code></p> </blockquote> <p>C++ runs on too many platforms for there to be good rules of thumb for this. Even on say x86 processors, differences in instruction set, # cores, cache sizes, CPU vendor / model / generation etc. can be overwhelming. Indirection is most costly if it results in memory page faults, and that's very dependent on the overall picture of program execution on the machine. If you care, benchmark real computers running the program until you find statistically relevant and stable results.</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