Re: What are the differences between std::shared_ptr and boost::shared_ptr?
On 17 Sep., 14:48, Juha Nieminen wrote:
SG <s.gesem...@gmail.com> wrote:
auto sa = std::make_shared<std::vector<int>>(100);
(*sa)[5] = 1729;
So you have, internally, a pointer which points to a dynamically
allocated object (plus probably another to a different dynamically
allocated object containing a refcount and other data), which has as
a member a pointer to a dynamically allocated array.
Double the indirection, double the amount of memory allocations,
double the overhead.
....compared to what? A fair comparison would be boost::shared_array.
Now, if Boost would offer a make_shared_array function I guess one
could reduce it to one allocation and cram the ref counter and int
array into one block of memory. But as far as I know there is no such
function (yet). I believe boost::shared_array<int>(new int[99]) also
requires two allocations.
Another possibility would be to use shared_ptr<array<int,99> > and use
make_shared. Also only one allocation. But the size is obviously fixed
at compile-time.
The poins is: Is it worth to add something like shared_array to the
standard when we have all these alternatives available? I think the
answer is no.
Cheers!
SG