Introduction

Due to its peculiar design and semantics, the standard smart pointer class auto_ptr was incompatible with other Standard Library components and was eventually deprecated. Shared_ptr was added to C++ programming in 2003 and has since replaced auto_ptr as the default smart pointer. However, it incurs noticeable performance overhead both in terms of size and speed. The C++0x Final Committee Draft (FCD) recently introduced another smart pointer called unique_ptr that combines the best of both worlds, it's very efficient (even more than auto_ptr), and yet it's fully compatible with the Standard Library, as is shared_ptr. That's not all, did you know that unique_ptr supports arrays too?

Performance Issues

The first question you'd ask yourself is probably: "Why do I need to learn how to use another smart pointer class when shared_ptr fits the bill?" In one word: performance. Shared_ptr uses reference counting to allow the sharing of a single resource by multiple objects. Additionally, its destructor and other member functions are virtual. These properties facilitate the customization of shared_ptr. However, the size of a typical shared_ptr object is 40 bytes, which is 10 times bigger than the size of a raw pointer on a 32-bit platform. The presence of virtual member functions means that in many cases, calls to member functions are resolved dynamically, incurring additional runtime overhead. These issues may not concern you if you're using shared_ptr sporadically. However, in time critical apps, or if you have containers that store a large number of shared_ptr objects, the performance overhead might be overwhelming. Unique_ptr gives you a safe and reliable smart pointer alternative that can compete with the size and speed of raw pointers. Here's how it's used.

Using Unique_ptr

Although unique_ptr is not a 100% source-compatible drop-in replacement for auto_ptr, everything you can do with auto_ptr, unique_ptr will do as well:

Unique_ptr lets you install a custom deleter if necessary. A deleter is a callable entity (a function, function object etc.) that the smart pointer's destructor will invoke to deallocate its resource. Unique_ptr's default deleter calls delete. If the resource isn't an object allocated by new you'll need to install a different deleter. For instance, a pointer allocated by malloc() requires a deleter that calls free():

Array Support

Unique_ptr can store arrays as well. A unique_ptr that owns an array defines an overloaded operator []. Obviously, the * and -> operators are not available. Additionally, the default deleter calls delete[] instead of delete:

Compatibility with Containers and Algorithms

You can safely store unique_ptr in Standard Library containers and let algorithms manipulate sequences of unique_ptr objects. For containers and algorithms that move elements around internally instead of copying them around, unique_ptr will work flawlessly. If however the container or algorithm uses copy semantics for its value_type you'll get a compilation error. Unlike with auto_ptr, there is no risk of a runtime crash due to a move operation disguised as copying:

Conclusion

In conclusion, unique_ptr is the safe equivalent of auto_ptr. Its small memory footprint and runtime efficiency make it a useful replacement for raw pointers. If you don't need to share resources, unique_ptr is the right choice for you.

Comments

There are no comments yet. Be the first to comment!

You must have javascript enabled in order to post comments.

Leave a Comment

Your email address will not be published. All fields are required.

Name

Email

Title

Comment

Top White Papers and Webcasts

Intelligent N+X Redundancy, Placement Affinities, & Future Proofing in the Virtualized Data Center
Virtualization brought about the ability to simplify business continuity management in IT. Workload portability and data replication capabilities mean that physical infrastructure failures no longer need impact application services, and they can rapidly be recovered even in the event of complete site failure.
However, Enterprises and Service Providers face new challenges ensuring they have enough compute …

Migrating away from Windows Server 2003 is an investment in your organization's future, and there has never been a better time to begin the migration process. Take the next step to transform your datacenter by upgrading your server platform with leading edge Windows Operating Systems and SanDisk flash solutions.