1

Starting with Boost release 1.53, shared_ptr can be used to hold a pointer to a dynamically allocated array. (http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/shared_ptr.htm)

Also, using shared_ptr has the benefit of allocator functions like boost::make_shared< int [] >(...);
Should I start using boost::shared_ptr instead of boost::shared_array when possible? it seems boost::shared_ptr can do most of the work boost::shared_array does.

5
  • One advantage of shared_array is that it overrides the [] operator. With shared_ptr<T[]> you'd need to dereference the pointer first: (*sharedPtr)[i] rather than sharedArray[i] Commented Feb 20, 2014 at 10:33
  • 1
    std::vector is often a better choice. Commented Feb 20, 2014 at 10:37
  • 2
    @Nick: No need for that, shared_ptr has support for operator[] for array types, which on the other hand hasn't support for operator-> or operator*. See boost.org/doc/libs/1_55_0/boost/smart_ptr/shared_ptr.hpp Commented Feb 20, 2014 at 11:33
  • Things have moved on since I was last using Boost! Commented Feb 20, 2014 at 11:44
  • @MaximYegorushkin: Thanks. maybe shared_ptr<std::vector> will solve most of the problems. Commented Feb 21, 2014 at 9:39

2 Answers 2

1

If the code is correct and working, I would not do the subject change.

If I had spare time and desire to do the change nevertheless, I would migrate to std::shared_ptr<T[]> straight away.

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

Comments

0

Yes and one important benefit of using shared_ptr is that you no longer need to release the memory explicitly. The shared_ptr is a smart pointer and hence deallocates the memory by himself hence avoiding any memory leak. Also there is similar question exist on SO please visit the link Why use one vs the other: `boost::shared_array` VS `boost::shared_ptr<std::vector>`?

1 Comment

shared_array, which author is using already is too a smart pointer

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.