Lecture 15a Persistent Memory & Shared Pointers

Similar documents
Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Pointers. Developed By Ms. K.M.Sanghavi

CSE 333 Lecture smart pointers

Homework 4. Any questions?

Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17

CSE 333 Lecture smart pointers

Lecture 4a Using The Standard Template Library. Jack Applin, Guest Lecturer

CA341 - Comparative Programming Languages

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

RAII and Smart Pointers. Ali Malik

CSE 333 Lecture smart pointers

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CS24 Week 3 Lecture 1

CS 376b Computer Vision

Common Misunderstandings from Exam 1 Material

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

COMP 2355 Introduction to Systems Programming

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Homework #3 CS2255 Fall 2012

Financial computing with C++

Short Notes of CS201

CSCI-1200 Data Structures Spring 2018 Lecture 25 Garbage Collection & Smart Pointers

CS201 Some Important Definitions

CS201 - Introduction to Programming Glossary By

2 2

Introducing C++ to Java Programmers

Program template-smart-pointers-again.cc

C++ Smart Pointers. CSE 333 Autumn 2018

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Vector and Free Store (Pointers and Memory Allocation)

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CS201 Latest Solved MCQs

CSE 333 Lecture 9 - intro to C++

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

CS11 Advanced C++ Spring 2018 Lecture 2

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Field Analysis. Last time Exploit encapsulation to improve memory system performance

Memory, Arrays, and Parameters

CS 241 Honors Memory

Object-Oriented Programming, Iouliia Skliarova

CS201- Introduction to Programming Current Quizzes

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

CSE 100: C++ TEMPLATES AND ITERATORS

Midterm Review. PIC 10B Spring 2018

COMP6771 Advanced C++ Programming

Exam duration: 3 hours Number of pages: 10

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

CPSC 427: Object-Oriented Programming

W3101: Programming Languages C++ Ramana Isukapalli

Assignment operator string class c++ Assignment operator string class c++.zip

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

C:\Temp\Templates. Download This PDF From The Web Site

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

There are, of course, many other possible solutions and, if done correctly, those received full credit.

C++ Constructor Insanity

Program template-smart-pointers.cc

Programming Assignment 2

C11: Garbage Collection and Constructors

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

Assertions and Exceptions

Object-Oriented Programming, Iouliia Skliarova

Programming C++ Lecture 2. Howest, Fall 2014 Instructor: Dr. Jennifer B. Sartor

Inheritance: Develop solutions by abstracting real-world object and their interaction into code to develop software solutions. Layering: Organization

Object-Oriented Principles and Practice / C++

CS360 Lecture 5 Object-Oriented Concepts

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

Objects Managing a Resource

Object-Oriented Programming for Scientific Computing

pointers + memory double x; string a; int x; main overhead int y; main overhead

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

CSCI-1200 Data Structures Fall 2009 Lecture 25 Concurrency & Asynchronous Computing

CE221 Programming in C++ Part 1 Introduction

CS 162, Lecture 25: Exam II Review. 30 May 2018

AN OVERVIEW OF C++ 1

Come and join us at WebLyceum

Dynamic Data Structures

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Written by John Bell for CS 342, Spring 2018

G Programming Languages - Fall 2012

Variables, Memory and Pointers

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Separate Compilation Model

C10: Garbage Collection and Constructors

A brief introduction to C++

CS 225. Data Structures. Wade Fagen-Ulmschneider

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Programming, numerics and optimization

C++ for Java Programmers

C++ for numerical computing

Assumptions. History

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Comp151. Construction & Initialization

Smart Pointers. Some slides from Internet

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Transcription:

Lecture 15a Persistent Memory & Shared Pointers Dec. 5 th, 2017 Jack Applin, Guest Lecturer 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 1

Announcements PA9 is due today Recitation : extra help (optional / ungraded) Final : same style as midterms Cumulative, but with emphasis on templates Code will be handed out on Thursday 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 2

Coming Full Circle Memory Management Our first major topic was memory management We outlined two strategies: Strategy 0: put everything on the stack Never leaks or double-deletes memory Doesn t work for dynamic data Doesn t work for persistent data Strategy 1: header classes Allows dynamic data to act like its on the stack Notice that STL exploits this for all containers Still doesn t work for persistent data So what do we do about persistent data? 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 3

Strategy #2 : Unique Pointers Unique Pointer strategy: Never have more than one pointer to heap objects Essence of the strategy: Disallow copying move allows copy, if source is nulled out Same with assignment (=) When a pointer falls out of scope, delete it Null out any unique pointer that is deleted To avoid double deletes Enforcing the strategy Copy constructor is private (compile time error) Destructor deletes the data if it falls out of scope std::move Makes a copy Nulls out source Assignment operator deletes destination, then nulls out source 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 4

Limitations of Unique Pointers Unique pointers are appropriate when data is Persistent Can be dynamic, too But what if its persistent and shared? Imagine the following surveillance system: Tracking module needs data as long as object is in view Identification module needs data until verified Graphics module needs to display object as long as tracking and/or identification is working on it 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 5

Memory Management Strategy #3: Shared Pointers std::shared_ptr<type> is a template Similar to std::unique_ptr<type> But this time, the semantics are that a pointer is shared among multiple users std::shared_ptr acts like a pointer Overloads * and -> Overloads copy constructor But copy constructor is public & available Copy constructor does not null out source Overloads destructor, assignment 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 6

Semantics: reference counting Shared pointers implement a type-specific reference counting scheme Used only for data stored in shared pointers More overhead than strategies 0 2 Only use it when you absolutely need it 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 7

Shared pointer pseudo-implementation std::shared_pointer<type> is a template So std::shared_pointer<document> defines a type This type has a hash table static variable Key is pointer address Value is reference count Supports type-specific garbage collection Reference counting Not stop-and-copy 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 8

Psuedo-implemention (cont.) std::shared_ptr<type*>(type* ptr) constructor Adds address to hash table With reference count 1 Copy constructor increments reference count Destructor Decrements the reference count Deletes data if reference count is now 0 Assignment operator Decrements old reference count (maybe deletes) Increments new reference count 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 9

Example 1. #include<memory> 2. using std::shared_ptr; 3. shared_ptr<document> AllocateDocument(char* filename) 4. { 5. shared_ptr<document> doc_ptr(new Document(filename)); 6. return doc_ptr; 7. } 8. void UseDocument(shared_ptr<Document>& doc_ptr) 9. { 10. cout << "Number_of_pointers = " << doc_ptr.use_count() << endl; 11. cout << "Address of Document in UseDocument is = " << &(*doc_ptr) << endl; 12. } 13. void DeAllocateDocument(shared_ptr<Document> doc_ptr) 14. { 15. cout << "Address of Document in DeAllocate is = " << &(*doc_ptr) << endl; 16. } 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 10

Example (cont.) 1. int main(int argc, char* argv[]) 2. { 3. shared_ptr<document> doc_ptr = AllocateDocument(20); 4. cout << "Address of Document = " << &(*doc_ptr) << endl; 5. cout << "Number_of_pointers = " 6. << doc_ptr.use_count() << endl; 7. UseDocument(doc_ptr); 8. 9. DeAllocateDocument(doc_ptr); 10. cout << "Address of Document = " << &(*doc_ptr) << endl; 11. cout << "Number_of_document ptrs = " 12. << doc_ptr.use_count() << endl; 13. return 0; 14. } 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 11

Questions on Example What do lines 5/6 of main print? Number of pointers = 1 What does UseDocument print from line 10? Number of pointers = 1 What do lines 11/12 of main print? Number of pointers = 1 Do lines 4 & 10 of main print the same thing? Yes What does line 11 in UseDocument print? The same thing 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 12

More Questions Does the address ever change? No, its not that kind of garbage collector Reference counting does not copy How do I handle my motivating example? Where I need to display an object until the other modules are done with it Hmm... 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 13

Weak Pointers Weak pointers are constructed from shared pointers std::weak_ptr<type*>(shared_ptr<type*> ptr) Part of memory management strategy #3 Weak pointers do not change the reference count Their constructors do not increment it Their destructors do not decrement it When referencing a weak pointer You can t. * and -> don t work. Construct a shared_ptr<> from it; use that. 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 14

Impact: Weak Pointers (II) The data might get deleted out from under them. Your code needs to check them every time they are used. Check via.expired(). 2017-12-04 CS253 Fall 2017 Jack Applin & Bruce Draper 15