Smart pointer to wrap array returned by a library

I'm running VC++ 2005 so std::unique_ptr is not available AFAIK. I use some library functions (black box) which effectively do return new T[n] and I want to wrap the result in some object which will mean I don't have to delete them in my destructor.

Does anything exist in the standard libraries circa VC++ 2008? If not, would the boost version of unique_ptr work perfectly as in this answer?

To clarify some confusion in the comments, the library function returns a raw pointer to a dynamically allocated array and I don't have access to change that... I need to take that pointer and wrap it in an object which automatically calls delete [] when the object goes out of scope.

Answers


In the pre-2011 standard library, all you've got is auto_ptr, which is suitable for returning from functions. However, it only works with single objects, not arrays, so it's not suitable for this use.

As far as I can tell, there isn't a unique_ptr in Boost. There is a scoped_ptr and scoped_array, but they aren't transferable and so can't be returned from a function.

boost::shared_array might be suitable, although it uses reference counting and so has a (hopefully small) overhead compared to a single-ownership pointer.

std::vector might be a good choice; as long as your compiler supports "return value optimisation" then you can (with a little bit of care) return one from a function without any unnecessary memory allocation or copying. However, it has the disadvantage that it doesn't prevent accidental copying of the data.

UPDATE: I've just noticed the words "black box" in your question, which I think might imply that the library functions return raw pointers, and you want to assign them to a smart pointer that will destroy them with delete [], and can't modify the functions themselves. If that's the case, boost::scoped_array should do the trick, and apologies to the people who deleted their answers in response to my comments.


It depends what you want to do with the resulting value.

In general, I would advise boost::scoped_ptr and boost::scoped_array in C++03. However note that they were crafted with a very specific mindset: to limit the scope of the resource. That is, they go further than just handling cleanup, they also guarantee that the resource will not leave the scope, ie cannot be transferred to the caller.

If you want more, it's not easy in C++03 (because of the absence of movable types). In this case, you might need to craft an "extended" boost::scoped_ptr, adding a release method for example to allow passing memory to the caller (which is then expecting to be taking care of it properly).


Need Your Help

Framework for unit test case in titanium appcelerator

ios titanium-alloy appcelerator-titanium

Any frameworks available for writing test cases to Support iOS applications developed using Appcelerator Titanium?

SQLite in Native Client

sqlite google-nativeclient

I am trying to use sqlite3 in a Native Client application. There is a port available in the Chromium project, but I was unable to make it run properly.