views:

251

answers:

5

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 editing 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.

+11  A: 

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
dirkgently
Good summation. I disagree that auto_ptr is fraught with issues. If yuo don't understand what it does then it can take you by surprise but that is solved by reading the documentation. Also the new C++0x provides std::unique_ptr which is practically a drop in replacement for auto_ptr but makes the move semantics more obvious to newcomers. It would also be nice to see which ones have been included into the new standard lib.
Martin York
Granted. I have been a bit harsh and we'll continue to live with `auto_ptr` (though my copy of the draft i.e. n3090 says it is deprecated).
dirkgently
Calling boost documentation "excellent" is a bit of a stretch :). The quality goes up and down, depending on the lib in question. But good answer in general, +1.
Lucas
@Lucas: Compared to what you get otherwise, yes, I do consider the documentation excellent. YMMV.
dirkgently
A: 

About the STL auto_ptr, I recommend reading Herb Sutter's (author of good C++ books) GuruOfTheWeek: http://www.gotw.ca/gotw/025.htm

+1  A: 

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.

PC2st
A: 

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.

DeadMG
A: 

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

David