shared_ptr implementation question
I have a question regarding shared_ptr implementation: currently shared_ptr is implementated so, that it contains: T * px; // contained pointer detail::shared_count pn; // reference counter I am interested to know philosophical thoughts why this kind of decision was made to implement the shared_ptr class this way? I assumed to save space which is consumed by such a pointer one could move T pointer (T*) to shared_count class. All shared_ptr classes would contain only one instance or (may be better pointer to counter): detail::shared_count* pn; // reference counter and the approach to retrieve pointer would be: pn->px //since px would be moved to pn This would reduce the size of shared_ptr<T> to sizeof(detail::shared_count*) which is at least by sizeof(detail::shared_count) smaller of may be more if alignment has to used. Are there any arguments which prevent shared_ptr from such implementation scenario? With Kind Regards, Ovanes Markarian
On Wed, 23 Aug 2006, Ovanes Markarian wrote:
I have a question regarding shared_ptr implementation:
currently shared_ptr is implementated so, that it contains:
T * px; // contained pointer detail::shared_count pn; // reference counter
I am interested to know philosophical thoughts why this kind of decision was made to implement the shared_ptr class this way?
I assumed to save space which is consumed by such a pointer one could move T pointer (T*) to shared_count class. All shared_ptr classes would contain only one instance or (may be better pointer to counter):
detail::shared_count* pn; // reference counter
and the approach to retrieve pointer would be:
pn->px //since px would be moved to pn
This would reduce the size of shared_ptr<T> to sizeof(detail::shared_count*) which is at least by sizeof(detail::shared_count) smaller of may be more if alignment has to used.
Are there any arguments which prevent shared_ptr from such implementation scenario?
One reason could be a question of performance. Placing 'px' in 'pn' would require an extra dereference to access the object, as you mention aboce, and such operations can be costly (that is one reason we usually implement multi-dimensional arrays as a one-dimensional array with computed offsets: multiplication is usually faster than derefence). -- François Duranleau LIGUM, Université de Montréal
"Ovanes Markarian"
I have a question regarding shared_ptr implementation:
currently shared_ptr is implementated so, that it contains:
T * px; // contained pointer detail::shared_count pn; // reference counter
I am interested to know philosophical thoughts why this kind of decision was made to implement the shared_ptr class this way?
I assumed to save space which is consumed by such a pointer one could move T pointer (T*) to shared_count class. All shared_ptr classes would contain only one instance or (may be better pointer to counter):
detail::shared_count* pn; // reference counter
That scheme doesn't handle derived-to-base pointer conversions. You can't make this work unless you have a separate pointer to the base object: struct A { int x; virtual ~A(){} }; struct B { int x; virtual ~B(){} }; struct C : A,B {}; shared_ptr<C> c(new C); shared_ptr<A> a(c); shared_ptr<B> b(c); -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (3)
-
David Abrahams
-
François Duranleau
-
Ovanes Markarian