Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
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.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 6 Vectors and arrays: Arrays: Run the following code. Anything unusual? #include using namespace std; #define N 10 #define M 11 int main() { int.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Pointers OVERVIEW.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
C++ Memory Overview 4 major memory segments Key differences from Java
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.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Object-Oriented Programming in C++
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
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:
CS 1704 Introduction to Data Structures and Software Engineering.
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 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
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.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
1 Introduction to Object Oriented Programming Chapter 10.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Memory Management.
Pointers and Dynamic Arrays
Pointers, Polymorphism, and Memory Allocation
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
group work #hifiTeam
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Overview of Memory Layout in C++
Chapter 15 Pointers, Dynamic Data, and Reference Types
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
9-10 Classes: A Deeper Look.
Dynamic Memory.
Dynamic Memory CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
9-10 Classes: A Deeper Look.
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared outside scope of any function or those declared as static – see examples on p. 591). Run time stack: locale variables, pass by value parameters (See figure on p. 592). Heap: for pointers. new operator gets memory from the heap. Sometimes called dynamic memory allocation.

See common errors p Avoid dangling pointers p Avoid buffer overflows p. 597.

Memory leaks. See Figure on p. 600 relates to logic on p. 599; See code on next slide. Run with and without the delete p command. Look at windows task manager (Ctrl- Alt-Delete) and look at process and performance tabs while running.

#include using namespace std; class myClass { private: long data[100000]; public: myClass(int i); long getData(int i); }; myClass::myClass(int i) {data[i]=12;} long myClass::getData(int i) { return data[i];} int main() { myClass *p; for (int i=0; i<10000; i++) { p = new myClass(i); cout getData(i) << endl; //cin.get(); //delete p; } cin.get(); return 0; }

Make sure nothing else points to an object when you delete it. Do not delete an element more than once or one that was never allocated. Example (Remove the comments from the code on the next slide):

class ClassA { private: int a; public: ClassA(int i); int getData(); }; ClassA::ClassA(int i) {a=I;} ClassA::getData() { return a;} int main() { ClassA *p1 = new ClassA(12); ClassA *p2 = p1; cout getData() << endl; //delete p1; cout getData() << endl; //delete p2; cin.get(); return 0; }

Demonstrate the code from the snippets file (class Demo). Discuss the results of the second set of output commands.

Copy constructor (p. 608) constructor that has one argument – an object of the same class the constructor is in. It’s used to create a copy of an object.

Explain why the demo does not work as expected and then include the copy constructor below: Demo(const Demo& d) { first = d.getFirst(); second = new int[5]; for (int i=0; i<5; i++) second[i] = d[i]; }

Focus on the code after the statement "y=x". There is still a problem – and a memory leak. Objects x and y are sharing dynamic memory. How do we have x and y correspond to different dynamic memory? Ans: Overload the “=“ operator.

Demo09 contains the author’s newly define String class. NOTE: That Visual C++ already has a String class when building a managed application. This demo is unmanaged. Discuss implementation and run to the printReverse() method. Problem solve the printReverse() method.

A few things to note: Note the two overloaded [] operators on pp Recall the need for two such operators discussed in the previous chapter. See common error on p.605. If you do not define a copy constructor there is a System-defined copy constructor. Quality tip on p However it might NOT be want you need!!

Uses When one object is declared and “set equal to” another object of the same class all on the same line (See demo09). When an object is “passed by value” to a function (See function printReverse() in demo09). Note: change the function parameter to “pass by reference” and the copy constructor is NOT called. Or Include the copy constructor on the next slide.

String Copy Constructor String::String(const String& right) { len = right.length(); buffer = new char[len+1]; for (int i=0; i<len; i++) buffer[i] = right[i]; buffer[len] = '\0'; }

SHOULD always define your own copy constructor when your class uses dynamic memory. Why!! Use debugger to show what is happening with and without copy constructors.

Shallow copy One object is a copy of another, including the SAME memory address in pointer variables. They share dynamically allocated data.

Deep copy Objects do NOT share dynamically allocated memory The contents of the dynamically allocated memory are the same in both objects. One is a copy of the other.

Note how the = operator is overloaded. The code WILL work without this overloaded operator so why include it? ANS. To remove any dynamically allocated memory and make sure deep copies are created if that is the intent. Note the quality tip on p Can use messages or put in breakpoints. I recommend this!!

Destructors: never explicitly invoked. Called at the end of a block when an object goes out of scope. at the end of functions (for any arguments or locals that were constructed). When dynamically allocated memory, object variable or object from a derived class is deleted when main finishes.

Note the difference between destruction and deletion (p. 615). Note the common error p. 617 – This will be relevant later. Note the quality tip p The Big Three. Destructors, copy constructors, and assignment operator. IMPORTANT!!!. If you do not provide them, the system will. The result is may be incorrect!!!

Note code snippets on p Exercise: implement that in demo09. Mention overloading new and delete operators, p We will not do this.

Destructor code is important for properly managing garbage collection. Poorly written destructors can result in memory leaks or dangling pointers. Delete dynamic memory but only if that memory is not referenced anywhere else. Discuss Reference Counting, p. 622.