smart pointers in c++11 - andreas buhr · smart pointers in c++11 9/15 std::unique_ptr i only one...

Post on 10-Jul-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Smart Pointers in C++11The very basics

Andreas Buhr 17. June 2015living knowledgeWWU Münster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 2 /15

std::shared_ptr<T>

std::shared_ptr<T>

I boost::shared_ptr<T> since version 1.16 (2000)I tr1::shared_ptr<T> in standard (2007)I std::shared_ptr<T> in C++11 (2011)

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 3 /15

Motivation

Automatic object lifetime management:I ... no memory leaksI ... no dangling pointersI ... no double frees

Widely adopted: Some companies have “no new, no delete” codingpolicies.

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 4 /15

Interface

#include <memory>class Foo { ... };

// creation:std::shared_ptr<Foo> my_foo_ptr(new Foo(...));

// usage:my_foo_ptr->some_function();function_using_Foo(*my_foo_ptr);

// automatic delete

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 5 /15

shared_ptr: How it Works

std::shared_ptr<Foo>

Foo*

refcount*

Foo

usecount

weakcount

refcount

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 6 /15

Things to know

I shared_ptr<T> is “as threadsafe as an int”I Overhead for creation (allocate refcounter)I Overhead for copy (update refcount, synchronization)I Overhead for destruction

... there is no free lunch!

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 7 /15

There is free lunch!

std::unique_ptr<T>

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 7 /15

There is free lunch!

std::unique_ptr<T>

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 8 /15

std::unique_ptr<T>

#include <memory>class Foo { ... };

// creation:std::unique_ptr<Foo> my_foo_ptr(new Foo(...));

// usage:my_foo_ptr->some_function();function_using_Foo(*my_foo_ptr);

// automatic delete

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 9 /15

std::unique_ptr<T>

I Only one pointer points to an object

I no copy constructorI no assignment operatorI but move constructor and move assignment operator

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 9 /15

std::unique_ptr<T>

I Only one pointer points to an objectI no copy constructorI no assignment operator

I but move constructor and move assignment operator

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 9 /15

std::unique_ptr<T>

I Only one pointer points to an objectI no copy constructorI no assignment operatorI but move constructor and move assignment operator

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 10 /15

std::unique_ptr<T> move assignment

std::unique_ptr<int> a(new int(42));std::unique_ptr<int> b;// a now holds a pointer to 42; b is emptyb = std::move(a);// now b holds the pointer to 42; a is empty

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 11 /15

unique_ptr: How it Works

std::unique_ptr<Foo>

Foo*

Foo

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 12 /15

class Foo{ ... };Foo* get_foo(void);int treat_foo(Foo* theFoo);

Exactly the same binary code!

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 12 /15

class Foo{ ... };Foo* get_foo(void);int treat_foo(Foo* theFoo);int myfunction(void){

std::unique_ptr<Foo> myfoo(get_foo());int result = treat_foo(myfoo.get());return result;

}

Exactly the same binary code!

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 12 /15

class Foo{ ... };Foo* get_foo(void);int treat_foo(Foo* theFoo);int myfunction(void){

std::unique_ptr<Foo> myfoo(get_foo());int result = treat_foo(myfoo.get());return result;

}int myfunction(void){

Foo* myfoo = get_foo();int result = treat_foo(myfoo);delete myfoo;return result;

}

Exactly the same binary code!

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 12 /15

class Foo{ ... };Foo* get_foo(void);int treat_foo(Foo* theFoo) noexcept;int myfunction(void){

std::unique_ptr<Foo> myfoo(get_foo());int result = treat_foo(myfoo.get());return result;

}int myfunction(void){

Foo* myfoo = get_foo();int result = treat_foo(myfoo);delete myfoo;return result;

}

Exactly the same binary code!

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 12 /15

class Foo{ ... };Foo* get_foo(void);int treat_foo(Foo* theFoo) noexcept;int myfunction(void){

std::unique_ptr<Foo> myfoo(get_foo());int result = treat_foo(myfoo.get());return result;

}int myfunction(void){

Foo* myfoo = get_foo();int result = treat_foo(myfoo);if(myfoo != nullptr)

delete myfoo;return result;

}

Exactly the same binary code!

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 12 /15

class Foo{ ... };Foo* get_foo(void);int treat_foo(Foo* theFoo) noexcept;int myfunction(void){

std::unique_ptr<Foo> myfoo(get_foo());int result = treat_foo(myfoo.get());return result;

}int myfunction(void){

Foo* myfoo = get_foo();int result = treat_foo(myfoo);if(myfoo != nullptr)

delete myfoo;return result;

}Exactly the same binary code! ,

,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 13 /15

std::unique_ptr<T> to std::shared_ptr<T>

You can transfer ownership from a std::unique_ptr<T> to astd::shared_ptr<T>

std::unique_ptr<int> a(new int(42));std::shared_ptr<int> b;b = std::move(a);

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 13 /15

std::unique_ptr<T> to std::shared_ptr<T>

You can transfer ownership from a std::unique_ptr<T> to astd::shared_ptr<T>

std::unique_ptr<int> a(new int(42));std::shared_ptr<int> b;b = std::move(a);

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 14 /15

Advanced Topics (not today)

I make_sharedI make_uniqueI custom deleterI enable_shared_from_thisI weak_ptr

,,

Andreas Buhr

livingknow

ledge

WWUMünster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER Smart Pointers in C++11 15 /15

Take Home

1. Use smart pointers!

2. Make std::unique_ptr your default.

,,

Andreas Buhr

top related