boost::shared_ptr and boost::shared_array

boost::shared_ptr is an excellent library, life is so much easier with it (but that is nothing new right…)

Anyway, I wanted to do this:

typedef boost::shared_ptr<short> MyPointer;
MyPointer bla(new short[500]);

That compiles but is not legal! We use new[] and shared_ptr uses the regular ‘delete’ instead of ‘delete[]’. Oh no!

So what’s the solution? Just use boost::shared_array instead!

typedef boost::shared_array<short> MyPointer;
MyPointer bla(new short[500]);

Now the new[] and delete[] calls match..

See also for a nice article about shared_ptr / array_ptr.