6

I want to use BOOST Smart pointer for memory management in my application. But I'm not sure which smart pointer should I use for dynamically allocated array shared_ptr or shared_array.

According to the BOOST doc Starting with Boost release 1.53, shared_ptr can be used to hold a pointer to a dynamically allocated array.

So I'm just wondering now what purpose user should use shared_array instead of shared_ptr.

5
  • Why not std::vector for dynamic arrays and std::unique_ptr for objects? Commented Sep 22, 2014 at 9:55
  • I have a 3rd party library functions which are taking raw pointer as argument, so I can't use vector. Commented Sep 22, 2014 at 10:46
  • 2
    std::vector has the same contiguous layout like a plain array. std::vector::data() and &vec[0] allow you to extract the pointer to that array. Commented Sep 22, 2014 at 11:22
  • True, but just curious is there any disadvantage to use vector over share_ptr. Commented Sep 22, 2014 at 11:26
  • 1
    Quite to the contrary, shared_ptr should only ever be used as a last resort option. Commented Sep 22, 2014 at 11:58

1 Answer 1

13

Before boost 1.53, boost::shared_ptr is to be used for a pointer to a single object.

After 1.53, since boost::shared_ptr can be used for array types, I think it's pretty much the same as boost::shared_array.

But for now I don't think it's a good idea to use array type in shared_ptr, because C++11's std::shared_ptr has a bit different behavior on array type compared to boost::shared_ptr.

See shared_ptr to an array : should it be used? as reference for the difference.

So if you want your code compatible with C++11 and use std::shared_ptr instead, you need to use it carefully. Because:

  • Code look like below gets compile error (you need a custom deleter), while boost's version is OK.

     std::shared_ptr<int[]> a(new int[5]); // Compile error
    
     // You need to write like below:
     std::shared_ptr<int> b(new int[5], std::default_delete<int[]>());
    
     boost::shared_ptr<int[]> c(new int[5]); // OK
    
  • However, if you write code like below, you are likely to get segment fault

     std::shared_ptr<T> a(new T[5]); // segment fault
     boost::shared_ptr<T> b(new T[5]); // segment fault
    

The syntax of shared_ptr in std and boost are different and not compatible.

Additional information: consider boost::ptr_vector, which is a pretty fast implementation for dynamic allocated objects in vector. Just FYI in case you want this feature.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I agree that to make it C+11 compatible I should go either with custom deleter option or boost shared_array but I'm looking for any additional benefits which I'll get after using shared_array over shared_ptr.
I don't know if any additional benefits exist for shared_array. And in the future C++ may introduce shared_ptr<T[]>, then it will be compatible with boost. But above all, it's better to think if you really need shared_array, maybe a scoped array is enough to suits the need, the you can use std::unique_ptr<T[]> instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.