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

24
Smart Pointers in C++11 The very basics Andreas Buhr 17. June 2015 living knowledge WWUMünster WESTF˜LISCHE WILHELMS -UNIVERSIT˜T MNSTER

Upload: others

Post on 10-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

Smart Pointers in C++11The very basics

Andreas Buhr 17. June 2015living knowledgeWWU Münster

WESTFÄLISCHEWILHELMS-UNIVERSITÄTMÜNSTER

Page 2: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 3: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 4: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 5: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 6: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 7: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 8: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 9: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 10: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 11: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 12: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 13: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 14: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 15: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 16: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 17: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 18: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 19: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 20: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 21: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 22: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 23: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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

Page 24: Smart Pointers in C++11 - Andreas Buhr · Smart Pointers in C++11 9/15 std::unique_ptr I Only one pointer points to an object I no copy constructor I no assignment operator

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