Boost logo

Boost Users :

From: Stuart Dootson (stuart.dootson_at_[hidden])
Date: 2006-01-11 14:04:16


On 1/11/06, yinglcs2_at_[hidden] <yinglcs2_at_[hidden]> wrote:
> Hi,
>
> I have a non-growable, size determined by run-time
> array.
> For example convert this from Java to C:
> int[] func(int size) [
> int[] array = new int[size];
>
> return array;
> }
>
> I was told the only way I can do that is using STL
> vector.
> But if I need to have a smart pointer for this STL
> vector, should I use the c++ auto_ptr? or the Boost's
> shared_ptr with STL vector or Boost's shared_array?
>
> Thank you.
>

You could use any of these - there are advantages and disadvantages....

std::auto_ptr - no dependency on Boost - but the assignment semantics
can be confusing at first - personally, I'd not use this.

So, this leaves boost::shared_array and std::vector. std::vector is
(IMO) the easier to use. However, when you return a std::vector from a
function, it (including it's storage) will be copied. When you return
a boost::shared_array, you effectively only copy the pointer, not the
storage. This makes boost::shared_array slightly more time&space
efficient IF you call this function a lot.

(Note: if your compiler has return value optimisation, returning a
std::vector may not incur a vector copy - but I wouldn't bank on
having it)

Personally, I would tend to use vector, because of the extra
facilities it provides - specifically, it carries its size around with
it, rather than me having to manage it!

Stuart Dootson


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