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

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Analysis of programs with pointers. Simple example What are the dependences in this program? Problem: just looking at variable names will not give you.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
PZ10B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10B - Garbage collection Programming Language Design.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Memory Management Tom Roeder CS fa. Motivation Recall unmanaged code eg C: { double* A = malloc(sizeof(double)*M*N); for(int i = 0; i < M*N; i++)
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 61C L07 More Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Specialized Reference Counting Garbage Collection using Data Structure Annotations By Eric Watkins and Dzin Avots for CS 343 Spring 2002.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Garbage collection (& Midterm Topics) David Walker COS 320.
Linked lists and memory allocation Prof. Noah Snavely CS1114
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
CSE333 SECTION 7. Midterm Debrief Hex View 1. Find a hex editor. 2. Learn ‘goto offset’ command. 3. See HW3 pictures. The header: Magic word Checksum.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
C++ Memory Overview 4 major memory segments Key differences from Java
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
Object-Oriented Programming in C++
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
1 Workin’ with Pointas An exercise in destroying your computer.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Heap storage Dynamic allocation and stacks are generally.
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Memory Management -Memory allocation -Garbage collection.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 12: Automating Memory Management
Memory Management CSCI 2720 Spring What is memory management? “the prudent utilization of this scarce resource (memory), whether by conservation,
1 Programming Languages (CS 550) List Processing, Dynamic Memory Allocation and Garbage Collection Jeremy R. Johnson.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Object Lifetime and Pointers
CS345 – Event-driven Programming
Storage 18-May-18.
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Storage Management.
CS 153: Concepts of Compiler Design November 28 Class Meeting
C++ Memory Management Idioms
Making Dynamic Memory Allocation Safer
Smart Pointers.
Chapter 9: Pointers.
Destruction and Copying
Dynamic Memory Management
Heap storage & Garbage collection
List Allocation and Garbage Collection
Destruction and Copying
Dynamic Memory Allocation
Linked List Functions.
Heap storage & Garbage collection
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Dynamic Memory Management
Rule of Three Part 1 & 2.
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Automating Memory Management
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

Smart Pointers

Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory

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

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

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

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

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

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

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

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

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

Heap of Fish bonn.de/IV/martini/Lehre/Veranstaltungen/SS00/InformatikII /JavaSimulation/HeapOfFish.html bonn.de/IV/martini/Lehre/Veranstaltungen/SS00/InformatikII /JavaSimulation/HeapOfFish.html

Compaction Yellow fish are garbage:

Still Problem Still can't make a new Red Fish

Much Better Compacting "live" objects reduces fragmentation:

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

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

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

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

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

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

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

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

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

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

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

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