How to create a smart pointer for a non-growable, size determined by run-time array
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. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
On 1/11/06, yinglcs2@yahoo.com
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
On 11/01/06, Stuart Dootson
std::auto_ptr - no dependency on Boost - but the assignment semantics can be confusing at first - personally, I'd not use this.
I'd just like to point out that std::auto_ptr calls delete, not delete[], on the pointer it wons, so it cannot be used with new[]ed arrays.
Personally, I would tend to use vector
Agreed 100% -- the reason there's no std::auto_array ( or similar ) class is because that's what std::vector is there for. - Scott
On 1/12/06, me22
On 11/01/06, Stuart Dootson
wrote: std::auto_ptr - no dependency on Boost - but the assignment semantics can be confusing at first - personally, I'd not use this.
I'd just like to point out that std::auto_ptr calls delete, not delete[], on the pointer it wons, so it cannot be used with new[]ed arrays.
Quite right - noticed just after I pressed the Send button - I'll use the defence that I never use std::auto_ptr :-) <snip>
- Scott
Stuart
participants (3)
-
me22
-
Stuart Dootson
-
yinglcs2@yahoo.com