Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 397
Amir Rachum Avatar asked Aug 14 '10 16:08

Amir Rachum


2 Answers

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
like image 129
dirkgently Avatar answered Sep 27 '22 22:09

dirkgently


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.

like image 20
Sadeq Avatar answered Sep 28 '22 00:09

Sadeq