mercredi 5 décembre 2018

Shared pointer Physical layout

This is more of a question about the physical design of a shared pointer. As mentioned on CPP Reference, a std::shared_ptr contains:

  1. A pointer to an object - generally owned one but might not be in the case of the aliasing constructor.
  2. Pointer to the control block - which itself further points to the owned object.

As a result the size of std::shared_ptr == size of 2 pointers.

Assuming that the general case of using std::shared_ptr involves the non-aliasing constructor, wont it be sufficient to have (Pointer -> CB -> Managed Object) i.e. the pointer points to the control block which in turn points to the managed object. It seems that the overhead involved with usage stays the same while, saving on space. Is this assertion correct? Also is there any shared pointer variant which is implemented as such?
This question is simply out of curiosity as I was reading in detail about smart pointers.

Aucun commentaire:

Enregistrer un commentaire