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

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 39. Copy Constructor.
Advertisements

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.
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.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
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.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
On dynamic memory and classes ● We previously had only discussed dynamic memory in regards to structs and dynamic arrays. ● However, they can be used (to.
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
CISC181 Introduction to Computer Science Dr
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
CS Computer Science IB: Object Oriented Programming
Pointers Revisited What is variable address, name, value?
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Dynamic Memory Allocation
CSC 253 Lecture 8.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Automatics, Copy Constructor, and Assignment Operator
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Automatics, Copy Constructor, and Assignment Operator
Chapter 9 Classes: A Deeper Look, Part 1
Chapter 15 Pointers, Dynamic Data, and Reference Types
Copy Constructor CSCE 121 J. Michael Moore.
Constructors and destructors
Constructors and Destructors
Copy Assignment CSCE 121 J. Michael Moore.
Summary: Abstract Data Type
Constructors and Destructors
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
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.
9-10 Classes: A Deeper Look.
Destructor CSCE 121.
CS410 – Software Engineering Lecture #5: C++ Basics III
COP 3330 Object-oriented Programming in C++
A Deeper Look at Classes
Class: Special Topics 2 For classes using memory allocation
Pointers and References
Copy Assignment CSCE 121.
Copy Constructor CSCE 121.
Rule of Three Part 1 & 2.
9-10 Classes: A Deeper Look.
Copy Constructors and Overloaded Assignment
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

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

Objects Containing Dynamically Allocated Members

Objects Containing Dynamic Members 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; }; with the following constructor: MyClass::MyClass(int n){ size=n; d = new int[size]; } then the following declaration creats an object containing an array of 5 integers MyClass myobj(5); and this contains an array of 10 integers (note both objects belong to same class) MyClass myobj2(10); // 10-element array

Destructor however, when myobj goes out of scope, the dynamically allocated array is leaked why? destructor is a function that is called implicitly (without mentioning its name) 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; } no need to deallocate automatic variables destructor is never invoked explicitly and does not accept parameters note that destructor is called on every local object when function ends

Copy Constructor 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? the function is passed a reference to the same array - copy of the array is not created copy constructor is invoked implicitly when a new copy of an object is created: when function is passed an object by value when function returns an object copy constructor can be invoked at object declaration, how? copy constructor is not invoked when one object is assigned to another (more on that later)

Copy Constructor (cont.) 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; }; 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]; } note that copy constructor can access private members of the parameter object can be invoked at object declaration: MyClass newobj(myobj);

Assignment Overloading what do you think this operation does? secondObj=firstObj; copies the value of pointer leaking the array of the old object and not creating a copy of the array in the new object 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; }; 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]; }

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 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]; }

Stackable Assignment 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 note the return value of the function, it is a reference to an object returned object refers to object used in return-statement similar to pass-by-reference parameter

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

Objects with Dynamic Members Review What are dynamically allocated objects Objects containing dynamically allocated members? What may be potential problems with the latter? What are the big three operations? What is a destructor? Why is it needed? When is it executed? How is it declared/defined? What is a copy-constructor? Why is it needed? When is it executed? How is it declared/defined? 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? What is stackability of an operator? How can overloaded assignment be made stackable?