开发者

How to use C++ Smart Pointers?

I've been using C++ for some time now and I still don't feel very comfortable about using smart pointers and I've only been using them when editi开发者_运维百科ng some code that uses them, never in my own code (it might be worth to say that I'm a student).

Can you explain what are the types of smart pointers, how do they work and when to use them?

Also, what is the "protocol" when receiving or passing raw pointers in interfaces written by other people?

Thanks.


C++98 does not provide any smart pointers except auto_ptr which is fraught with its own issues. C++0X tries to fix this by bringing in a few more varieties (shared_ptr, unique_ptr etc.). In the meantime the best bet is to use Boost. Take a look at the various flavors available to you here. Boost is community driven, extensively tested and of course free. There is excellent documentation with sample code that will help you get started.

Can you explain what are the types of smart pointers, how do they work and when to use them?

There are a number of them. In short:

scoped_ptr <boost/scoped_ptr.hpp> Simple sole ownership of single objects. Noncopyable.

scoped_array <boost/scoped_array.hpp> Simple sole ownership of arrays. Noncopyable.

shared_ptr <boost/shared_ptr.hpp> Object ownership shared among multiple pointers.

shared_array <boost/shared_array.hpp> Array ownership shared among multiple pointers.

weak_ptr <boost/weak_ptr.hpp> Non-owning observers of an object owned by shared_ptr.

intrusive_ptr <boost/intrusive_ptr.hpp> Shared ownership of objects with an embedded reference count.

(That is from Boost documentation and note that they have containers for such pointers too!)

Also, what is the "protocol" when receiving or passing raw pointers in interfaces written by other people?

For me the most important rules are:

  • Const-qualification
  • Not to deallocate stuff I did not allocate
  • Check for transfer of ownership/move semantics


Smart Pointer types are an abstraction layer to automate the process of allocation and deallocation of memory, their constructor function, gets an allocated memory (via pointer) and their destructor function, frees the allocated memory. Of course the constructor and the destructor can be inlined (thus there is no overhead to call them). For example:

{
    int* raw = new int(40);
    auto_ptr<int> sp(raw); //inline constructor: internal_holder = raw
    //...
    //inline destructor: delete internal_holder
}

In C++ it's nice to use pointers indirectly (hide them behind the classes). The overhead of creating a new smart pointer is negligible. But shared_ptr is more weighty due to its behavior for counting references (it is reference counted).

When we want to use raw pointers which is received from other functions which is written by other people, if this raw pointers shouldn't freed by ours self, then we shouldn't use Smart Pointers.


There is no rule about when to use smart pointers. More appropriately, you use smart pointers wherever possible. Raw pointers are a rarity in well-written C++ code. When you receive a raw pointer, wrap it in a self-freeing custom written smart-pointer, if it's your duty to deallocate it.


To complete the answers and the ones available with the upcoming C++0x standard. These links give examples on when and how they are used. It also documents the relationship between shared_ptr and weak_ptr.

http://www2.research.att.com/~bs/C++0xFAQ.html#std-shared_ptr

http://www2.research.att.com/~bs/C++0xFAQ.html#std-weak_ptr

http://www2.research.att.com/~bs/C++0xFAQ.html#std-unique_ptr


About the STL auto_ptr, I recommend reading Herb Sutter's (author of good C++ books) GuruOfTheWeek: use this Link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜