Getting started with smart pointers in C ++

I have a C ++ application that makes heavy use of pointers to support fairly complex data structures. The application performs mathematical modeling on huge data sets (which can take up several GB of memory) and compiled using Microsoft Visual Studio 2010.

Now I am recycling an important part of the application. To reduce errors (dangling pointers, memory leaks, ...), I would like to start using smart pointers. A donation of memory or performance is acceptable if it is limited.

In practice, most classes are supported in large pools (one pool per class), and although classes can refer to each other, you can consider the pool as the owner of all instances of this class. However, if the pool decides to delete the instance, I do not want any of the other classes to still refer to the remote instance in order to have a dangling pointer.

In another part, I keep a collection of pointers to instances that are supplied by other modules in the application. In practice, other modules maintain ownership of the transferred instance, but in some cases the modules do not want to take care of the property and simply want to transfer the instance to the collection, telling it "now, manage it."

What is the best way to start implementing smart pointers? Simply changing pointers [in random order] using smart pointers does not seem right and probably does not deliver all (or any) of the benefits of smart pointers. But which method is better?

What types of smart pointers should be explored further? I sometimes use std :: auto_ptr to free locally allocated memory, but this seems to be fixed in C ++ 0x. Is std :: unique_ptr a better alternative? Or do I need to go to generic pointers or other types of smart pointers?

Question Replacing existing level pointers with smart pointers seems similar, but instead of asking how simple it is, I ask what the best approach is and what kind of smart pointers are best suited.

Thanks in advance for your ideas and suggestions.

+6
c ++ architecture smart-pointers
source share
2 answers

Here are 3 varieties found in the new C ++ 11 standard (unique_ptr replaces auto_ptr)

http://www.stroustrup.com/C++11FAQ.html#std-unique_ptr

http://www.stroustrup.com/C++11FAQ.html#std-shared_ptr

http://www.stroustrup.com/C++11FAQ.html#std-weak_ptr

You can read the text for each pointer, and there is an explanation when to use it. To manage local memory, unique_ptr is the choice. It is not copied, but movable, since you move it around the receiver, it acquires it.

Shared_ptr is used if you want to share an instance of an object where no one owns this object, and make sure that it is not deleted, and someone else has a link to it. After the last user of the object destroys the shared_ptr container, the contained object will be deleted.

weak_ptr is shared with shared_ptr. It allows you to “lock” to see if the shared_ptr reference object exists before trying to access the internal object.

+1
source share

I recommend using unique_ptr whenever possible (this may require some analysis of the program) and shared_ptr when this is not possible. If in doubt, use shared_ptr to ensure maximum security: when transferring control to the container, the reference counter simply goes to two and then back to one, and the container will eventually delete associated object automatically. When performance becomes a problem, consider using boost::intrusive_ptr .

+3
source share

All Articles