 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Dynamic Allocation Eric Roberts CS 106B February 4, 2013.
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.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
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.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
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.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
Pointers OVERVIEW.
C++ Memory Overview 4 major memory segments Key differences from Java
Object-Oriented Programming in C++
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
C++ REVIEW – POINTERS AND TEST DRIVEN DEVELOPMENT.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Writing Correct C++ Programs without “delete” Huang-Ming Huang CSE332 Guest Lecture Washington University in St. Louis.
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.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Dynamic Storage Allocation
Pointers and Dynamic Arrays
Overview 4 major memory segments Key differences from Java stack
CS 3370 – C++ String, Vectors, and Arrays
C++ Memory Management Idioms
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
This pointer, Dynamic memory allocation, Constructors and Destructor
Overview 4 major memory segments Key differences from Java stack
Basic C++ What’s a declaration? What’s a definition?
understanding memory usage by a c++ program
Overview of Memory Layout in C++
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Indirection.
Practical Session 4 Rule of 5 R-value reference Unique pointer
Dynamic Memory Management
Destructor CSCE 121 J. Michael Moore.
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Destructor CSCE 121.
Dynamic Memory.
Pointers and References
Dynamic Memory CSCE 121.
Dynamic Memory Management
Presentation transcript:

 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools

 The new and delete operators  C++ does not have garbage collection  but it does have deterministic destructors!  Can deallocate any resource automatically  not just memory!  e.g., can unlock a mutex or close a connection  when a local object goes out of scope  when you use delete  no need for finally

 Two versions:  Scalar (single object):  T* p = new T;// Calls constructor  delete p;// Calls destructor  Array:  T* p = new T[n];// p points to first element  delete [ ] p;  delete style must match the allocation ([ ])  Failing to delete is a memory leak

 “Resource Acquisition is Initialization”  Memory is just one of many resources  Treat all resources equally in C++:  Have a constructor acquire them  Have the destructor release them  Example: file streams  Example: raii.cpp

 Objects that emulate pointers  They hold the real pointer  but the wrapper object lives on the stack  its destructor calls delete on the real pointer  Overloaded operators:  *  ->

 It runs twice!  First: it must return a “pointer-like thing”  Next: operator-> is called again on that return value  Eventually a raw pointer must be returned struct Foo {int x; int y;}; class FooWrapper { Foo* pf; public: FooWrapper(Foo* p) : pf(p) {} Foo* operator->() { cout << "returning a Foo*\n"; return pf; } }; int main() { Foo f = {1,2}; FooWrapper fw(&f); cout x << '\n'; cout y << '\n'; } /* Output: returning a Foo* 1 returning a Foo* 2 */

 Creating a simple smart pointer  SafePtr.cpp (generic version)  smart.cpp (multi-level)  unique_ptr (uniqptr1-3.cpp, deleter1.cpp)  unique_ptrs are not copyable  shared_ptr (sharedptr.cpp, deleter2.cpp)  shared_ptrs increment their reference count when copied  And delete the raw pointer when count == 0

  not implemented in gcc :-( 

 Does the following before returning a pointer:  Allocates needed memory on the heap ▪ calls the library function operator new( )  Initializes the object by calling the proper constructor

 Does the following before returning a pointer to the first element:  Allocates needed memory on the heap ▪ calls the library function operator new[ ]( )  Initializes each object by calling the proper constructor

 Does 2 important things:  Calls the destructor for the object  Returns the memory to the free store ▪ via the library function void operator delete(void*)

 Calls the destructor for each object in the array  Returns the memory to the free store  via void operator delete(void*);  You must use delete [ ] for arrays

 You can overload all 4 memory functions:  operator new(size_t)  operator new[ ](size_t)  operator delete(void* )  operator delete[ ]( void*)  Can be useful for tracing memory operations  The array versions are seldom used  See memory.cpp

 You can manage heap on a class basis  Just provide operator new( ) and operator delete( )  As member functions  Must be static ▪ Because they’re stand-alone functions, of course ▪ Even if you don’t declare them so, they will still be static  Example: memory2.cpp

 If you want to disallow allocating object on the heap, declare non-public class allocation functions: protected: void* operator new(size_t){return 0;} void operator delete(void*) {}  (It is an interesting mystery that on some platforms, bodies are required for these functions when you declare them)

 A special-purpose version of the new operator used by library developers  see Lab 3  (you will be tested on it)