C++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Dynamic memory allocation
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.
Chapter 6 Data Types
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Classes and Objects: Chapter 8, Slide 1 Object and its encapsulation.
DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CS Advanced C++ Exception Handling Topic #5.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Chapter 3.5 Memory and I/O Systems. Memory Management 2 Only applies to languages with explicit memory management (C, C++) Memory problems are one of.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter 3.7 Memory and I/O Systems. 2 Memory Management Only applies to languages with explicit memory management (C or C++) Memory problems are one of.
CS-2303 System Programming Concepts
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Object Oriented Programming Elhanan Borenstein Lecture #4.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
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.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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:
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
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.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). SDD document framework should be set up in your Wiki by now.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Today: –Two simple binding examples. –Function Hiding.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Memory Management.
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Constructor & Destructor
This pointer, Dynamic memory allocation, Constructors and Destructor
Constructors and Destructors
9-10 Classes: A Deeper Look.
Data Structures and Algorithms Memory allocation and Dynamic Array
DYNAMIC MEMORY MANAGEMENT
9-10 Classes: A Deeper Look.
Object and its encapsulation
SPL – PS3 C++ Classes.
Presentation transcript:

C++ 程序语言设计 Chapter 12: Dynamic Object Creation

Outline  Object creation process  Overloading new & delete

Why create dynamic object?  Sometimes you know the exact quantity, type, and lifetime of the objects in your program.  But not always. an air-traffic system handle planes an CAD system use shapes a network have notes

Dynamic Memory Allocation in C  C has always provided the functions malloc( ) and free( ) (along with variants of malloc( )) that allocate storage from the heap (also called the free store) at runtime. see the file - MallocClass.cpp

Problems come from “DMA” 1.forget to initialize a object. 2.accidentally do something to the object before you initialize it. 3.hand the wrong-sized object. Improper initialization is responsible for a large portion of programming problems, so it’s especially important to guarantee constructor calls for objects created on the heap.

Object creation process  When a C++ object is created, two events occur: 1.Storage is allocated for the object. 2.The constructor is called to initialize that storage.  Step 2 always happens.  Step 1 can occur in: the static storage area-managed by compile the stack-managed by compile the heap-managed by yourself

operator new  combine all the actions necessary to create an object into a single operator called new in C++.  When you create an object with new: 1.allocates enough storage on the heap to hold the object 2.calls the constructor for that storage

operator new MyType *fp = new MyType(1,2); the equivalent of malloc(sizeof(MyType)) is called the constructor for MyType is called with the resulting address as the this pointer using (1,2) as the argument list * The default new checks to make sure the memory allocation was successful before passing the address to the constructor.

operator delete  When you destroy an object with delete: 1.calls the destructor 2.releases the memory (often with a call to free( ))

operator delete requires the address of an object delete can be called only for an object created by new If the pointer you’re deleting is zero, nothing will happen, so recommend setting a pointer to zero immediately after you delete it see the file - NewAndDelete.cpp

delete void* is probably a bug  if you call delete for a void*, operator delete should not call a destructor. see the file - BadVoidPointerDeletion.cpp delete a knows that a points to an Object, the destructor is called and thus the storage allocated for data is released delete b releases the storage for the Object b, but the destructor is not called so there is no release of the memory that data points to So you get a very quiet memory leak

new & delete for arrays  In C++, you can create arrays of objects on the heap, and (of course) the constructor is called for each object in the array, so there must be a default constructor.  MyType* const fp = new MyType[100];  MyType* const fp2 = new MyType; fp is actually the starting address of an array, so it makes sense to select array elements using an expression like fp[3].

new & delete for arrays  what happens when you destroy the array?  delete fp2;  delete fp; The destructor will be called for the MyType object pointed to by the given address, and then the storage will be released  delete []fp; to give the compiler the information that this is actually the starting address of an array

Running out of storage  What happens when the operator new( ) cannot find a contiguous block of storage large enough to hold the desired object? A special function called the new- handler is called The default behavior for the new-handler is to throw an exception

Running out of storage  replace the new-handler by including new.h and then calling set_new_handler( ) with the address of the function you want installed.  The new-handler function must take no arguments and have a void return value. see the file - NewHandler.cpp

Overloading new & delete The constructor and destructor calls are never under your control (otherwise you might accidentally subvert them), but you can change the storage allocation functions operator new( ) and operator delete( ). 1.efficiency: creating and destroying so many objects of a particular class that it has become a speed bottleneck 2.heap fragmentation: By allocating objects of different sizes it’s possible to break up the heap so that you effectively run out of storage

Overloading new & delete  you’re changing only the way raw storage is allocated. The compiler will simply call your new instead of the default version to allocate storage, then call the constructor for that storage. delete has a similar limitation.

Overloading global new & delete  If you overload the global versions, you make the defaults completely inaccessible – you can’t even call them from inside your redefinitions. see the file - GlobalOperatorNew.cpp

Overloading global new & delete The overloaded new must take an argument of size_t, it is the size of the object you’re responsible for allocating The overloaded new must return a pointer either to an object of that size (or bigger, if you have some reason to do so), or to zero if you can’t find the memory (in which case the constructor is not called!). The return value of operator new( ) is a void*, not a pointer to any particular type The operator delete( ) takes a void* to memory that was allocated by operator new

Overloading new & delete for class  When the compiler sees you use new to create an object of your class, it chooses the member operator new( ) over the global version.  The global versions of new and delete are used for all other types of objects (unless they have their own new and delete). see the file - Framis.cpp

Overloading new & delete for arrays  If you overload operator new and delete for a class, those operators are called whenever you create an object of that class.  if you create an array of those class objects, the global operator new( ) is called to allocate enough storage for the array all at once, and the global operator delete( ) is called to release that storage.  You can control the allocation of arrays of objects by overloading the special array versions of operator new[ ] and operator delete[ ] for the class. see the file - ArrayOperatorNew.cpp

Constructor calls MyType* f = new MyType;  What happens if the storage allocation in new fails?  The constructor is not called in that case, so although you still have an unsuccessfully created object, at least you haven’t invoked the constructor and handed it a zero this pointer see the file - NoMemory.cpp

placement new & delete  There are two other, less common, uses for overloading operator new( ): 1.You may want to place an object in a specific location in memory. 2.You may want to be able to choose from different allocators when calling new  The overloaded operator new( ) can take more than one argument: 1.the first argument is always the size of the object 2.the other arguments can be anything you want X* xp = new(a) X;

placement new & delete  How to destroy the object ? can call the destructor can not release the memory by the dynamic memory mechanism because it wasn’t allocated on the heap  The answer is a very special syntax. You can explicitly call the destructor xp->X::~X(); see the file - PlacementOperatorNew.cpp

next… Inheritance & Composition thanks!