Take the 2-minute tour ×

I've had a hard time finding references in the TR1 documentation concerning shared arrays. The Boost documentation is fairly clear that there is a significant difference between the C++ "new" and "new[]" expressions. The shared_ptr template is meant to correctly hold a pointer to a dynamically allocated objected created using "new". The shared_array template is meant to correctly hold a pointer to a dynamically allocated array using "new[]".

I'm in the process of updating some code to use the TR1 shared_ptr template and associated functions, but I've found no mention of shared_array. Does the TR1 shared_ptr implementation differentiate between "new" and "new[]", and destroy those pointers correctly? As far as I can tell from looking at the TR1 spec, it appears it does not. If this is the case, should I still be using the boost shared_array template for "new[]" style allocations?

share|improve this question
add comment

2 Answers

up vote 23 down vote accepted

That is correct, there is no shared_array in TR1.

You can, however, provide your own deleter object to perform "delete []" if you wish using this constructor:

template<class Other, class D>
   shared_ptr(Other* ptr, D dtor);

For example:

template<typename T>
struct my_array_deleter
   void operator()(T* p)
      delete [] p;

shared_ptr<int> sp(new int[100], my_array_deleter<int>());
share|improve this answer
this looks good on the surface, but does shared_ptr provide an operator[]? I don't think boost's does. –  Evan Teran Mar 9 '09 at 23:37
TR1 shared_ptr does not. You can always "int* p = sp.get(); p[5] = 42;" Are you sure you need shared_array though? Vector or string are often sufficient. –  Brian Neal Mar 10 '09 at 13:32
but if sp declared as shared_ptr<int> then you will get() not int*, but int? –  Yola Dec 21 '11 at 11:16
@Yola - I'm not sure I understand your question. sp.get() returns the underlying raw pointer that sp manages. –  Brian Neal Dec 21 '11 at 15:23
@Brian Neal, sorry, i was confused get() with dereference - *sp. +1 –  Yola Dec 21 '11 at 15:41
add comment

I suspect that most people who use TR1 do not use arrays, but use vector<> instead.

I haven't read TR1, so I'll answer on the basis of Boost, which is probably good enough. boost::shared_ptr<> deals with individual objects, and not arrays. That's what boost::shared_array<> is for.

If you're using arrays, and have reasons to convert to shared_array<> but not to vector<>, use shared_array<>.

share|improve this answer
But even boost::shared_ptr<> can be instantiated with a custom deleter that could do a "delete []". This, I believe, is why TR1 did not adopt boost::shared_array. –  Brian Neal Mar 9 '09 at 19:37
well, c++1x also hasn't adopted shared_array. i don't know why. however, they have an unique_ptr<T>. if you use unique_ptr<T[]>, then it uses delete[] to free. i don't know why they don'T provide they for shared_ptr too. like shared_ptr<int[]> . –  Johannes Schaub - litb Mar 9 '09 at 21:44
add comment

Your Answer


