SatView: Pointer Perfect, part 3.5
(Page 1 of 4 )
In this article in our ongoing series on pointers, J. Nakamura talks about some additional smart pointers, their advantages and drawbacks, and when and where you would employ them.
In the previous article we saw that the usage of pointers is not entirely trivial, if only proven by the numerous garbage collectors developed for today’s modern programming languages. The usage of smart pointers can help make the usage of pointers in C++ a lot safer, although the std::auto_ptr provided by the STL has some counter-intuitive properties.
The Boost library (www.boost.org) provides additional smart pointers, all with their advantages and drawbacks. Still you will find that they will cover most of the situations where you would like to employ a smart pointer. I highly recommend you check Boost out, since it offers lots more than just smart pointers.
In this article I will only try to motivate you to use these smart pointers. It is not my goal to provide a complete description of the interfaces… please go to www.boost.org for the latest documentation instead.
BOOST::SCOPED_PTR.
This is the simplest of all the smart pointers Boost offers. It basically behaves the same as const std::auto_ptr and its name makes its intention quite clear: it frees the allocated memory it owns when it goes out of scope (by leaving a function or by moving on to the next iteration of a loop for example), or by calling its reset() function.
Being simple, the scoped_ptr comes without shared-ownership or transfer-of-ownership semantics. You should understand by now that because of this behavior, we cannot store a scoped_ptr in (STL) containers.
The scoped_ptr is actually so simple that it doesn’t need any member variables and has therefore no more space overhead than that of a built-in pointer. The code example of the previous article can easily be adopted to the usage of a boost::scoped_ptr:
void foo() {
boost::scoped_ptr<MyClass> pMyObj(new MyClass);
/* perform some operations here */
}
The main reason why you should prefer using this pointer over the const std::auto_ptr is that its name makes it clear when the pointer is being deleted. While the auto_ptr allows you to transfer ownership, the scoped_ptr strictly forbids it. It is also possible to free the object pointed at and store a new pointer in a scoped_ptr, while the equivalent usage of a const auto_ptr will prohibit you from doing this. The scoped pointer is pretty useful to use as a member of a class to ensure that the resource it is holding is freed when the object is destructed.
scoped_ptr cannot correctly deal with a pointer to a dynamically allocated array. In one of the previous articles you saw that the new[] operator has to be matched with the delete[] operator, or otherwise the result would be undefined and most likely result in only the first object in the array being deleted. The scoped_ptr uses the delete operator in its destructor to free the object; thus it cannot correctly destruct dynamically allocated arrays.
Use the boost::scoped_array to manage pointers to dynamically allocated arrays. Its behavior is like that of a scoped_ptr with the difference that the delete[] operator is used in its destructor and it doesn’t implement the operator* and operator-> , but the operator[] instead.
Next: BOOST::SHARED_PTR. >>
More C# Articles
More By J. Nakamura