Boost logo

Boost Users :

Subject: Re: [Boost-users] question about boost pointer
From: Lars Viklund (zao_at_[hidden])
Date: 2012-06-08 11:50:28


On Fri, Jun 08, 2012 at 08:33:11AM -0700, young wrote:
> Can I define:
>
> int a[100];
> boost::shared_ptr<int> p(a);
>
> and get next int by using p?

No, and that's wrong in several ways.

A shared_ptr<T> conceptionally owns a single pointer of type T*, and is
responsible for when destruction time comes, either invoke 'delete' or
call the provided deleter function.

If you want to refer to storage owned somewhere else, you would have to
pass in a 'no-op' deleter in order to avoid multiple or incorrect
destruction.

If you want to own a sequence-of-T, like the ones allocated by 'new
T[]', you would use a shared_array<T>.

And yet again, the whole point of a shared_ptr and shared_array is
shared ownership and responsibility among the instances for the
pointed-to object or objects. If you start referring externally owned
data, at best you're just giving users false hopes, and at worst causing
accesses through dangling pointers or even multiple/incorrect
destruction.

-- 
Lars Viklund | zao_at_[hidden]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net