Post

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:

1
2
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!

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

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

See also http://www.onlamp.com/lpt/a/6559 for a nice article about shared_ptr / array_ptr.

This post is licensed under CC BY 4.0 by the author.