Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

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.
Introduction to Programming Lecture 39. Copy Constructor.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Dynamic Allocation Eric Roberts CS 106B February 4, 2013.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
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.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Classes Separating interface from implementation
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
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.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
 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.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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:
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
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.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
 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.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
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 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Learners Support Publications Constructors and Destructors.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Learning Objectives Pointers as dada members
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Class: Special Topics Copy Constructors Static members Friends this
CS Computer Science IB: Object Oriented Programming
Pointers Revisited What is variable address, name, value?
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
Copy Constructor CSCE 121 J. Michael Moore.
Constructors and Destructors
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.
CS410 – Software Engineering Lecture #5: C++ Basics III
COP 3330 Object-oriented Programming in C++
Pointers and References
Copy Constructor CSCE 121.
SPL – PS3 C++ Classes.
Presentation transcript:

Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function (invocation) frame? l what is a heap? how is it used? what is new and delete operations? How are they used? l how is dynamically allocated variable accessed? assigned value at declaration time? l what is memory leak? loose pointer (again)? l how are dynamic arrays allocated? deallocated? l how can a pointer be passed by value/by reference to a function? l what are dynamically allocated objects? 1

Objects Containing Dynamically Allocated Members

Objects Containing Dynamic Members l if we want to create an object that can shrink and grow as needed? class MyClass{ public: MyClass(int); // constructor private: int *d; int size; }; l with the following constructor: MyClass::MyClass(int n){ size=n; d = new int[size]; } l then the following declaration MyClass myobj(5); creates an object containing an array of 10 integer variables MyClass myobj2(10); // 10-element array 3

Destructor however, when myobj goes out of scope, the dynamically allocated array is leaked l destructor is a function that is called (automatically) when object goes out of scope class MyClass{ public: MyClass(int); // constructor ~MyClass(); // destructor private: int *d; int size; }; name of destructor tilde ( ~ ) and name of class MyClass::~MyClass(){ delete [] d; } l no need to deallocate automatic variables l destructor is never called explicitly and does not accept parameters l note that destructor is called on every local object when function finishes 4

Copy Constructor l what if we have this function (note that the object is passed by value): void myfunc (MyClass); if we invoke it as follows myfunc(myobj); what happens? l the function is passed a reference to the same array - copy of the array is not created l copy constructor is invoked automatically when a new copy of an object is implicitly created n when function is passed an object by value n when function returns an object l copy constructor can be used explicitly l copy constructor is not called when one object is assigned to another (more on that later) 5

Copy Constructor (cont.) l copy constructor is a constructor that accepts an object of the same class passed by reference class MyClass{ public: MyClass(int); // regular constructor MyClass(const MyClass&); // copy constructor private: int *d; int size; }; l defined as follows MyClass::MyClass(const MyClass& org){ size=org.size; d = new int[size]; for(int i=0; i< size; ++i) d[i]=org.d[i]; } l note that copy constructor can access private members of the parameter object can be invoked explicitly: MyClass newobj(myobj); 6

Assignment Overloading what do you think this operation does? secondObj=firstObj; l copies the value of pointer leaking the array of the old object and not creating a copy of the array in the new object l assignment needs to be overloaded assignment overloading operator accepts by reference the object on the right-hand-side of equation and is invoked by the object on the left-hand-side: lhsobj = rhsobj; class MyClass{ public: … void operator= (const MyClass&); private: int *d; int size; }; l can define as follows (not quite right yet): void MyClass::operator= (const MyClass& rhs){ size=rhs.size; delete [] d; d=new int[size]; for (int i=0; i < size; ++i) d[i]=rhs.d[i]; } 7

Self-Assignment Protection what happens if you do this assignment? myobj=myobj; It is legal in C++. Is our overloaded assignment going to handle it right? this is a reserved keyword. It is a pointer to the object that invokes the member function. It is passed implicitly to every member function l assignment overloading (still not quite right): void MyClass::operator= (const MyClass& rhs){ if (this != &rhs){ // if not same size=rhs.size; delete [] d; d=new int[size]; for (int i=0; i < size; i++) d[i]=rhs.d[i]; } 8

Stackable Assignment l what happens if you do this assignment? thirdObj = secondObj = firstObj; here is the definition that gets above code to work correctly MyClass& MyClass::operator= (const MyClass& rhs){ if (this != &rhs){ // if not same size=rhs.size; delete [] d; d=new int[size]; for (int i=0; i < size; i++) d[i]=rhs.d[i]; } return *this; // return lhs } l note the return value of the function, it is a reference to an object n returned object refers to object used in return-statement n similar to pass-by-reference parameter 9

The BIG Three l big three - copy constructor, overloaded assignment and destructor l expert opinion - if you need any one of them - most probably you will need all three l they are not syntactically related but it is usually safer to define all three if you think you’d need at least one 10

Objects with Dynamic Members Review l What are dynamically allocated objects Objects containing dynamically allocated members? What may be potential problems with the latter? l What are the big three operations? l What is a destructor? Why is it needed? When is it executed? How is it declared/defined? l What is a copy-constructor? Why is it needed? When is it executed? How is it declared/defined? l What is operation overloading? What is overloaded assignment? Why is it needed for objects with dynamic members? How is it declared/defined? What is this -pointer? What is protection against self-assignment? How is this -pointer used for that? l What is stackability of an operator? How can overloaded assignment be made stackable? 11