Brian sabbeth smart_pointers

32
STD::SMART POINTERS Brian Sabbeth May 9, 2013

Transcript of Brian sabbeth smart_pointers

Page 1: Brian sabbeth smart_pointers

STD::SMART POINTERS

Brian Sabbeth

May 9, 2013

Page 2: Brian sabbeth smart_pointers

MEMORY MANAGEMENT:

Page 3: Brian sabbeth smart_pointers

MEMORY MANAGEMENT:

Page 4: Brian sabbeth smart_pointers

MEMORY MANAGEMENT:

Page 5: Brian sabbeth smart_pointers

MEMORY MANAGEMENT:

Page 6: Brian sabbeth smart_pointers

MEMORY MANAGEMENT:

Amongst so many files shared objects can become a

problem for pointer related issues

Page 7: Brian sabbeth smart_pointers

MEMORY MANAGEMENT:

Amongst so many files shared objects can become a

problem for pointer related issues

Page 8: Brian sabbeth smart_pointers

SMART POINTERS

Page 9: Brian sabbeth smart_pointers

SMART POINTERS

AbstractDatatypes

Simulate Pointers

Automatic Resource Deallocation

Page 10: Brian sabbeth smart_pointers

SMART POINTERS

AbstractDatatypes

Simulate Pointers

Automatic Resource Deallocation

Page 11: Brian sabbeth smart_pointers

SMART POINTERS

#include <memory> defines general utilities to manage

dynamic memory:

std::shared_ptr

std::unique_ptr

“They work by means of operator overloading, the behavior of traditional

(raw) pointers, (e.g. dereferencing, assignment) while providing additional

memory management algorithms.” ~wikipedia

Page 12: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

#include <memory>

Page 13: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

#include <memory>

Unique – Not allowed to be copied!

New objects can be moved or transferred between smart

pointers.

Page 14: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

#include <memory>

Unique – Not allowed to be copied!

std::unique_ptr<int>uniqPtr 1(new int(123) );

New objects can be moved or transferred between smart

pointers.

Page 15: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

#include <memory>

Unique – Not allowed to be copied!

But can be moved.

EASY TO IMPLEMENT:

std::unique_ptr<int> uniqPtr1(new int(123) );

123

*

uniqPtr1

Page 16: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

#include <memory>

Unique – Not allowed to be copied!

But can be moved.

EASY TO IMPLEMENT:

std::unique_ptr<int> uniqPtr1(new int(123) );

123

*

uniqPtr1

*

std::unique_ptr<int> uniqPtr2 = uniqPtr1;

uniqPtr2

Page 17: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

#include <memory>

Unique – Not allowed to be copied!

But can be moved.

EASY TO IMPLEMENT:

std::unique_ptr<int> uniqPtr1(new int(123) );

123

*

uniqPtr1

*

std::unique_ptr<int> uniqPtr2 = uniqPtr1;

uniqPtr2

Page 18: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

#include <memory>

Unique – Not allowed to be copied!

But can be moved.

EASY TO IMPLEMENT:

std::unique_ptr<int> uniqPtr1(new int(123) );

123

*

uniqPtr1

std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);

Page 19: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

#include <memory>

Unique – Not allowed to be copied!

But can be moved.

EASY TO IMPLEMENT:

std::unique_ptr<int> uniqPtr1(new int(123) );

123

*

uniqPtr1

std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);

Page 20: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

#include <memory>

Unique – Not allowed to be copied!

But can be moved.

EASY TO IMPLEMENT:

std::unique_ptr<int> uniqPtr1(new int(123) );

123

*

uniqPtr1

std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);

Page 21: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

#include <memory>

Unique – Not allowed to be copied!

But can be moved.

EASY TO IMPLEMENT:

std::unique_ptr<int> uniqPtr1(new int(123) );

123

std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);

Page 22: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

#include <memory>

Unique – Not allowed to be copied!

But can be moved.

EASY TO IMPLEMENT:

std::unique_ptr<int> uniqPtr1(new int(123) );

123

std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);

uniqPtr2

*

Page 23: Brian sabbeth smart_pointers

STD::UNIQUE_PTR

Unique Pointers are returnable

EASILY DONE!

std::unique_ptr<int>returnUnique(intx);

{

unique_ptr<int>my_unique_ptr(newint(x));

return my_unique_ptr;

}

This returns a local variable which will be

destroyed within the function when return is

read

The pointer itself handles the return as if it is

a “move.”

Page 24: Brian sabbeth smart_pointers

*

STD::SHARED_PTR

#include <memory>

std::shared_ptr<int> sharedPtr1(new int(123) );

123sharedPtr1

1

Ref Count

Page 25: Brian sabbeth smart_pointers

*

STD::SHARED_PTR

#include <memory>

std::shared_ptr<int> sharedPtr1(new int(123) );

123sharedPtr1

1

Ref Count

Page 26: Brian sabbeth smart_pointers

*

STD::SHARED_PTR

#include <memory>

std::shared_ptr<int> sharedPtr1(new int(123) );

123sharedPtr1

2

Ref Count

std::shared_ptr<int> sharedPtr2(sharedPtr1);

Page 27: Brian sabbeth smart_pointers

*

STD::SHARED_PTR

#include <memory>

std::shared_ptr<int> sharedPtr1(new int(123) );

123sharedPtr1

1

Ref Count

Page 28: Brian sabbeth smart_pointers

STD::SHARED_PTR

#include <memory>

Shared!

123

0

Ref Count

Page 29: Brian sabbeth smart_pointers

STD::SHARED_PTR

#include <memory>

Shared!

0

Ref Count

Page 30: Brian sabbeth smart_pointers

STD::SHARED_PTR

#include <memory>

Memory is free

Page 31: Brian sabbeth smart_pointers

STD::SHARED_PTR

Page 32: Brian sabbeth smart_pointers

STD::SMART_POINTERS

Bibliography:

Lavavej, Stephen T. "C9 Lectures:.” Microsoft, n.d. Web. 06 May 2013.

"Smart Pointer." Wikipedia. Wikimedia Foundation, 04 June 2013. Web.

06 May 2013.

C Reference. N.p., n.d. Web. 06 May 2013.