Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-11-02 07:02:51

From: "Zhang, Gary" <Gary.Zhang_at_[hidden]>
> Can somebody tell me why the reference count is incremented
> when shared_ptr is initialized by a null ( 0 ) pointer ?

Because the reference count, as currently specified, can never be zero. It
can only go to zero when the shared_ptr is being destroyed.

> Isn't it more logical for ref_count to remain 0 since
> NULL pointer has no sharing value ?

In the current implementation, there are two things that are shared, the
object (there is none when the pointer is NULL) and the count, which always
exists, and is being shared even between NULL shared_ptrs.

It is possible to implement a different shared_ptr, one that has no count
(pn == 0) when px == 0. This implementation is a bit more complicated, and
it's not clear what the benefits are.

An advantage of the current specification is that the part of shared_ptr
that manages the object lifetime does not interpret the pointer value in any
way, i.e. it does not need to recognize NULL being a special, non-shared

Peter Dimov
Multi Media Ltd.

Boost list run by bdawes at, gregod at, cpdaniel at, john at