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.
Each vector
occupies a fixed size in the containing object, independent of the number of elements currently stored by the vector
. It is likely that the value_type
of the vector
(e.g. vector<int>
has value_type
int
) won't affect the size of the contained vector
object itself: only the amount of heap-allocated store the vector
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).
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?
Inserting elements into A
can only ever cause existing elements in A
to be moved (when the capacity is exceeded). B
and C
can never be affected.
Is that then an argument for pointers? Or are vectors internally only a pointer to the allocated memory?
YES, it's an argument... such a good one that YES, vector
s already do use pointers to the contiguous memory where the actual value_type
elements are stored.
Same question would go for lists etc...
YES, list
s store their value_type
elements on the heap too, and the size of the object embedding or derived from list
is unaffected by operations on the list
.
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?enter code here
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.