Some questions about shared_ptr, scoped_ptr and shared_array

advertisements

I have some questions about smart pointers implemented in boost library. Is the only diffrence between shared_ptr and scoped_ptr that scoped_ptr doesn't have copy constructor and shared_ptr has it? Should i use then scoped_ptr instead of shared_ptr always when object doesn't call copy constructor? I also doesn't understand idea of shared/scoped array. Can't I just use std::vector instead of it?


Is the only diffrence between shared_ptr and scoped_ptr that scoped_ptr doesn't have copy constructor and shared_ptr has it?

The difference is more fundamental than that; it has to do with how the smart pointers own the object it points to. What makes smart pointers different from dumb pointers is that the concept of ownership is a core component of their function. The ownership semantics is what differentiates the different kinds of smart pointers.

Because smart pointers "own" the thing they point to, they can do useful things like deleting objects when the smart pointers go away (this is made possible using only the rules of the language; no compiler magic is needed). This way, memory management can be made almost automatic in C++ (despite claims to the contrary, there's very little manual memory management required in modern C++).

  • shared_ptr implements reference-counting semantics for memory management. Multiple shared_ptrs can own a single object. A shared_ptr going away does not necessarily delete the object it points to because there may be another shared_ptr owning the object. The last shared_ptr that owns an object and goes away will delete the object it owns.

  • scoped_ptr implements exclusive-ownership semantics. Only one scoped_ptr can own any one object. When a scoped_ptr goes away, it will always delete the object it owns (because there's only one owner). It's typically used as a lightweight RAII mechanism for objects allocated on the free store.

The array versions (shared_array and scoped_array) have essentially the same semantics, but are designed specifically for arrays e.g. they use delete[] instead of delete, implements the array subscript operator, etc.

shared_ptr and shared_array also allows you to specify a custom deleter, if the default delete behavior is not appropriate for the object. scoped_ptr and scoped_array do not have that ability, since they are quite lightweight compared to shared_ptr and shared_array.

In C++11, the newest and current version of C++, there's also a unique_ptr, which is just like scoped_ptr except that you can transfer the ownership of an object to another unique_ptr. In C++03, an older but more widely supported version of C++, there's auto_ptr which is equivalent to unique_ptr except it was easy to use it in an unsafe manner by accident (which is why it is deprecated in C++11).

Should i use then scoped_ptr instead of shared_ptr always when object doesn't call copy constructor?

Which one you choose doesn't depend on the presence of the copy-constructor, since shared_ptr and scoped_ptr does not require the object to be copy-constructible. You pick the one depending on the required ownership semantics. If the object will have multiple owners, you use shared_ptr. If the object will only have one owner and the object's existence lasts only within a scope, use scoped_ptr (hence the name scoped_ptr).

I also doesn't understand idea of shared/scoped array. Can't I just use std::vector instead of it?

std::vector does not implement reference-counting semantics like shared_array does. std::vector is more like scoped_array, but can be copied. When you copy a std::vector, you also copy all of the elements it contains. That's not the case for scoped_array. std::vector also has functions that allow you to manipulate and examine its contents (e.g. push_back, insert, erase, etc.), but is much more heavyweight than scoped_array.