I was wondering: How will the shared_ptr type required by the upcoming
C++ standard be typically implemented?
An "easy" implementation is to have shared_ptr have two pointers
inside it, one to point to the data, and another to point to the
reference number, which the class allocates as necessary. However,
technically this is way more inefficient than it could be. It needlessly
makes the size of the class two pointers big, and the separate
allocation of one single integer value (for the reference count) wastes
memory.
A much more efficient implementation would be if the reference count
was somehow attached to the allocated data itself. This way the
shared_ptr can consist of one single pointer and it doesn't need to
allocate the reference count separately. Of course this would require
for the memory allocator to always allocate the additional space
required for the reference count (which would be wasted everywhere where
memory is allocated dynamically but shared_ptr is not used).
Are there other ways of doing this efficiently?
I am curious to know what kind of implementation will be expected for
shared_ptr in future compilers, the simple but wasteful one, or a more
efficient one.
How is it implemented in the boost library?
I am actually interested in knowing a more efficient way of
implementing this than the "easy" way I described above. Any pointers
(no pun intended)?