Scott Lurndal said:
In general, generalizing about the use of pointers is complex and error-prone.
Not true at all.
I don't see how the number of objects has any bearing on pointer-vs-reference.
Noone was talking about pointer vs reference. The discussion was about
dynamic vs automatic allocation.
The number of objects is the same regardless of how they are accessed.
It's not about how they are accessed, but about how they are created and
destroyed.
The number of objects only has an effect on performance if the memory space
the objects reside in is constrained (i.e. one begins to page objects out
to make room for others).
There is certainly a difference between one single big allocation for a
compound object vs several small allocations for each subobject.
If the objects are small (and pointers generally getting larger), this is
especially true.
This is true regardless of pointer or reference to object.
This exhibits a lack of understanding of the underlying processor; whether the
object is accessed via a "C++ Reference" or via a "Pointer", the generated
code will be usually identical. Particularly in RISC systems. There are some cases
where pointer-based access _may_ result in an extra instruction when compared
to a reference, but only on CISC architectures where non-load-store instructions
allow direct access to memory ; even in this case, the overhead will be subsumed
by noise and won't be measurable.
The point is not reference vs pointers, but direct access vs indirect
access (pointer or reference).
Direct object access also permits more optimizations (like inlining)
because the dynamic type of the object is always identical to the static
type.
Yes, there is some (quite minor, often O(1)) overhead for allocation and
deallocation.
And this is exactly the point. It is usually O(1) regarding the size of the
allocated memory for one call. But it's O(n) regarding the number of
allocations.
This means, the size of the objects does not matter, the number of
allocations does.
Also, the asymptotical performance with respect to the allocation size is
not really relevant, since C++ objects don't tend to be that large. The
constant factor will dominate and it is certainly not neglectable for
dynamic allocation.
On the other hand, using dynamic allocations (in conjunction with lookaside lists
and :

perator new) can often result in better cache locality and higher L1/2/3
cache hit rates for certain algorithms due to the ability of the application
programmer to control memory placement.
I really doubt that it's often. For general purpose programming the
locality provided by direct members is probably almost always the best you
can get. And it's for free, no custom allocators or obscure new operators
needed.
Use what makes sense for your application.
Ignore blanket statements like "don't use pointers in C++".
Agreed.
Tobi