Eli Bendersky-s website C++11 using unique_ptr with standard… — Volkswagen Pointer I

8 Июн 2015 | Author: | Комментарии к записи Eli Bendersky-s website C++11 using unique_ptr with standard… — Volkswagen Pointer I отключены
Volkswagen Pointer I

C++11: using unique_ptr standard library containers

20th, 2012 at 8:46 pm

C++11, the only smart available in the standard C++ library was . Alas, auto_ptr isn’t smart. It has very problematic semantics that make it to use in several important scenarios. In auto_ptr can not be used with containers and algorithms !

auto_ptr is charitably characterized as a valiant to create a unique_ptr before C++ had semantics.

auto_ptr is now deprecated, and not be used in new code. When you get a try doing a global search-and-replace of to unique_ptr in your code

So what is this unique_ptr and what can it be used for?


To put it simply, unique_ptr be the default smart pointer by new C++ code, replacing raw pointers as as possible. unique_ptr cleanly the single ownership idiom – it be copied and assigned, and it cleans up the object when it’s

Here’s some code to this [1] :

The lines marked the ERROR! comment won’t compile. The compiler will saying something like:

In to managing the pointed object’s unique_ptr provides the other capabilities of a smart pointer: it operator* and operator- . provides a to obtain the raw pointer ( get ), to relinquish of the pointed object ( release ), and to the object it manages ( reset ). It lets you customize the way the pointed is deleted (if you don’t want it to be the delete operator), and has some niceties – just consult favorite C++ reference.

What sources and sinks?

In this I want to focus not on the grocery of unique_ptr ‘s features, but its interesting semantics . Specifically, given unique_ptr forbids copying and one may wonder how it can fit in the source and sink which is so useful for smart

In other words, we’d this to work:

And in C++11, it Even though unique_ptr be copied, it can be moved . Move are a perfect match for unique_ptr – the two reinforce each other. move semantics, unique_ptr is safe and efficient. Here’s code to demonstrate this:

Again, there’s a line with ERROR! here – it once again that a can’t be copied. However, it can be moved, as the next line [2]. When the erroneous is commented out, this prints:

Note how cleanly the is being passed between the in this code. At each in time, only a single owns the pointed Foo object. this is efficient – the actual object only gets once and destructed once.

– motivation

So unique_ptr is a useful smart pointer. But what it really shine (especially compared to auto_ptr ) is that it can be in standard containers.

Why is it so important to be to place smart pointers containers? Because holding by value is sometimes very Containers, especially when with algorithms, tend to objects around. Large are expensive to copy, hence like to keep pointers to inside containers instead.

What follows is a very example that demonstrates It shows how much more it is to sort a vector of large that are stored by value, it is when they’re stored by [3] .

First, let’s create a large object that has defined ordering properties by numeric ID:

We also need a to compare two such objects. we need two – one for a container that object by value, and another for the version:

Let’s now create two and populate them with objects:

Finally, we’ll the two vectors with the standard algorithm, and measure the runtime for large n :

Volkswagen Pointer I

The timing results I get are consistent – the by-pointer sorting is faster than the by-value [4]. That’s a very difference, and it’s all due to the copying has to do for moving the objects around the container.

So holding objects of size inside standard is not a good idea in terms of But holding raw pointers to them is not so great, because of all the safety that come with raw The container can’t own the pointed because its destructor will destruct the pointer, which nothing. So the calling code has to own the objects which are being around by the container. Add exceptions early returns to the mix, and is a recipe for memory leaks or worse problems.

What we’d really to do is let our objects be managed by a smart and put that into a container. would guarantee a clean strategy – the container destroys its when it gets destroyed – just the way it should be. This is why is so exciting.

Containers of unique_ptr

the by-pointer version of the code to hold unique_ptr is very First, we need another function:

And then we just to create the vector . populate it and sort it, similarly to the way we’ve for the other vectors:

That’s it! And the performance? Almost to the by-pointer version (I measured of 1-5%, depending on the data).

about shared pointers?

smart pointer C++11 with it is the shared_ptr / weak_ptr implementing a reference-counted approach to ownership. While much flexible than unique_ptr . is slower and consumes more managing the reference count is not [5] .

Which one to use depends on your needs, but I agree with Sutter’s proposal of using by default and switching to shared_ptr if the arises.

In addition, it is my personal that preferring unique_ptr a certain memory management on the code, since you know at point exactly who owns Shared pointers give you a of security you can over-use and end up with leaks, which are tricky to (just like when Python C extension code). shared pointers signal the of APIs less clearly owning pointers. When factory returns a shared does it mean it keeps a to the object too? With an pointer, the API is self documenting ( returns a unique_ptr . then is for sure giving away With a shared pointer, it not, and need external to clarify.


I have mentioned how references and move semantics can code more efficient C++11. unique_ptr is another example that makes me to use a C++11-capable compiler as soon as

unique_ptr provides an excellent mix of and safe memory management. it’s a great example of how ideas in language design to create a whole that is than its parts.

Volkswagen Pointer I
Volkswagen Pointer I


Other articles of the category "Pointer":

Our partners
Follow us
Contact us
Our contacts


Born in the USSR

About this site

For all questions about advertising, please contact listed on the site.

Volkswagen all cars catalog with specifications, pictures, ratings, reviews and discusssions about cars Volkswagen.