Presentation is loading. Please wait.

Presentation is loading. Please wait.

Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory.

Similar presentations


Presentation on theme: "Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory."— Presentation transcript:

1 Smart Pointers

2 Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory

3 Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory But they are tricky – Memory leaks – Bad pointers

4 References References : safer pointers – C++ : Non-nullable

5 References Java : – All objects are on heap – Stack has Numeric Variables References – References are nullable – Cannot delete memory

6 References Python Everything is a reference… even numbers x = 1 x 1

7 References Python Everything is a reference… even numbers x = 1 x = 2 x 1 2

8 References Python Everything is a reference… even numbers x = 1 x = 2 y = x x 1 2 y

9 References Python Everything is a reference… even numbers x = 1 x = 2 y = x x = 4 x 1 2 y 4

10 References Python Everything is a reference… even numbers x = 1 x = 2 y = x x = 4 x = x + 1 x 1 2 y 4 5

11 Garbage Collection Garbage collection: Automatically reclaim unused memory from heap Various strategies Reference counting Mark and Sweep

12 Heap of Fish http://web.informatik.uni- bonn.de/IV/martini/Lehre/Veranstaltungen/SS00/InformatikII /JavaSimulation/HeapOfFish.html http://web.informatik.uni- bonn.de/IV/martini/Lehre/Veranstaltungen/SS00/InformatikII /JavaSimulation/HeapOfFish.html

13 Compaction Yellow fish are garbage:

14 Still Problem Still can't make a new Red Fish

15 Much Better Compacting "live" objects reduces fragmentation:

16 Smart Pointers C++11 brought smart pointers – Pointers do reference counting – Memory released when last reference is released

17 Shared Ptr shared_ptr : counts number sharing the object – When shared_ptr leaves scope, count-- – Memory deleted when count == 0

18 Weak Ptr weak_ptr : keeps track of object without "counting" – Can not be used directly – Use to ask for a real shared ptr when needed – Used to avoid cycles in shared_ptr

19 Unique Ptr unique_ptr : unshared pointer – Can't copy can only move – Deletes memory when pointer leaves scope

20 Courses With Strong/Weak Course Manager responsible for loading from DB

21 Courses With Strong/Weak Student asks for pointers to courses Course manager creates objects, stores weak_ptr

22 Courses With Strong/Weak Student asks for pointers to courses Student given shared_ptrs

23 Courses With Strong/Weak Student asks for pointers to courses Student given shared_ptrs

24 Courses With Strong/Weak Student asks for pointers to courses New student asks for a new class

25 Courses With Strong/Weak Student asks for pointers to courses New student asks for an existing class CS162 now has refcount of 2

26 Courses With Strong/Weak Student asks for pointers to courses First student destroyed… shared_ptrs with refcount == 1 destroyed

27 Objective C Can pick: – Manual release – Garbage collection (on some platforms) – ARC : Automatic Reference Counting Like shared_ptrs and weak_ptrs


Download ppt "Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory."

Similar presentations


Ads by Google