Making Dynamic Memory Allocation Safer

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types
Advertisements

. Smart Pointers. Memory Management u One of the major issues in writing C/C++ code is managing dynamically allocated memory u Biggest question is how.
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.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Template class Wrapper { public: T* operator->() { return &myT; } private: T myT; }; int main() { Wrapper wThing; wThing- >Foo(); // calls Thing::Foo()...
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
C++ Memory Overview 4 major memory segments Key differences from Java
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory.
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Motivation and Overview
Lecture 9 Files, Pointers
Pointers, Polymorphism, and Memory Allocation
C++ Memory Management Idioms
Class: Special Topics Copy Constructors Static members Friends this
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Dynamically Allocated Memory
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 15 Pointers, Dynamic Data, and Reference Types
Smart Pointers.
Chapter 9: Pointers.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Overview of Memory Layout in C++
Constant pointers and pointers to constants
Chapter 12 Pointers and Memory Management
Dynamically allocating arrays within structures
CMPE 135: Object-Oriented Analysis and Design November 27 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
C++ Smart Pointers CSE 333 Spring 2018
Practical Session 4 Rule of 5 R-value reference Unique pointer
CISC/CMPE320 - Prof. McLeod
Memory Leaks and Dangling Pointers
Dynamic Memory Management
Objects Managing a Resource
C++ Smart Pointers CSE 333 Autumn 2018
9-10 Classes: A Deeper Look.
Dynamic Memory.
C++ Smart Pointers CSE 333 Winter 2019
Dynamic Memory Allocation
Introduction to Data Structures and Software Engineering
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Dynamic Memory CSCE 121.
Dynamic Memory Management
Automating Memory Management
9-10 Classes: A Deeper Look.
CS 144 Advanced C++ Programming April 30 Class Meeting
SPL – PS3 C++ Classes.
Presentation transcript:

Making Dynamic Memory Allocation Safer Smart Pointers Making Dynamic Memory Allocation Safer

What’s Wrong with Dumb (Raw) Pointers ownership unclear: declaration does not state whether it should be destroyed when done using unclear if points to scalar or array exactly once destruction is problematic: what if there are paths in program where pointer is not deallocated (memory leak) or doubly deallocated (unspecified behavior) exceptions are particularly known for escaping clear deallocation paths unclear if loose (dangles)

unique_ptr ensures there is a single pointer to dynamically allocated object, invokes destructor when goes out of scope need <memory> declaring unique_ptr<Myclass> p1(new Myclass(1)); // C++11 auto p2 = make_unique<Myclass>(1); // C++14 using dereference and arrow operator are supported cannot copy, assign, pass by value can std::move() p2 = std::move(p1); unique_ptr<Myclass> p3(std::move(p1)); can reassign: p1.reset(); p2.reset(new Myclass(1)); p3=nullptr; can check if assigned: if(p1) …., can compare can be used in STL (no copying algs.): std::vector<unique_ptr<int>> v; deallocating: destructor is called on owned object if pointer goes out of scope pointer gets reassigned pointer is assigned nullptr

shared_ptr keeps number of pointers pointing to object, deallocates when zero need <memory> declaring auto sp = std::make_shared<Myclass>(10); // C++11 using may copy, assign, pass by value may reassign, check if assigned, compare, use in STL deallocating calls destructor when last reference disappears (gets reassigned, goes out of scope)

Miscelany no naked new and delete: cause problems: int *p = new int(55); unique_ptr<int> p1 (p); unique_ptr<int> p2 (p); preferably no new/delete at all (possible for C++14) shared_ptr is more powerful but less efficient than unique_ptr unique_ptr is just a wrapper over raw pointer shared_ptr stores reference count, requires two memory lookups auto_ptr is depreciated and to be avoided shared_ptr may have a reference cycle that prevents objects from de-allocation. weak_ptr is not included in reference count, may be loose (dangle)