Boost logo

Boost :

From: Larry Evans (jcampbell3_at_[hidden])
Date: 2003-01-07 20:55:58


Peter Dimov wrote:
> From: "Larry Evans" <jcampbell3_at_[hidden]>

[snip]
>
> This doesn't look correct to me... did you mean something like
>
> struct X
> {
> Y * p;
>
> explicit X(Y * p): p(p) {}
> ~X() { delete p; }
> };
>
> struct Y
> {
> shared_ptr<X> p;
> };
>
> int main()
> {
> Y * py = new Y;
> shared_ptr<X> px(new X(py));
> py->p = px;
> px.reset();
> }
>
> ?
Yes. I had thought about the need to delete the Y* in the
X::DTOR, but that was days ago and I forgot to include it.

I also thought a little more about it and what I was suggesting
was more like:

struc Y;
struct X
{
    boost::unshared_cyclic_ptr<Y> y; //cyclic involves this arc.
    explicit X(void): y(new Y) {}
};
struct Y
{
    boost::shared_cyclic_ptr<X> x;
};

Where {unshared|shared}_cyclic_ptr use the detlef method to
record their offsets and hence enable tracing the pointer graph.

Then I'd imagine you'd ask, why not just use a shared_ptr<Y> instead
of bothering with unshared<Y>. The only answer I can think of is
that maybe that reflects better the meaning of the variable y.
The programmer knows it's never shared; so, he decides to
be explicit about it with unshared_ptr<Y>. If he had used
shared_ptr<Y>, then someone maintaining his code would have to wonder
who else is sharing this Y? Also, the overhead for unshared_ptr
would be at least a little less than shared_ptr (no reference count
or mark bit and no updating the reference count).


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk