Memory Management Issues, Solutions, and Examples.

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.
Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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.
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
Classes Separating interface from implementation
Classes: A Deeper Look Systems Programming.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
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.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
 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.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
C++ Memory Overview 4 major memory segments Key differences from Java
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 9 Classes: A Deeper Look, Part I Part II.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
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.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
Xiaoyan Li, CSC211 Data Structures Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Prof. Xiaoyan Li Department of Computer Science.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)
Learners Support Publications Constructors and Destructors.
Pointers and Arrays Dynamic Variables and Arrays.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Constructors and Destructors
The C++ programming language
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Constructors and Destructors
Indirection.
Dynamic Memory Management
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.
Dynamic Memory.
The C++ programming language
Data Structures and Algorithms Memory allocation and Dynamic Array
Class: Special Topics 2 For classes using memory allocation
CS 144 Advanced C++ Programming March 21 Class Meeting
Dynamic Memory Management
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
SPL – PS3 C++ Classes.
Presentation transcript:

Memory Management Issues, Solutions, and Examples

Issues in memory management General Issues –Which should manage memory? Library Vs. application using the library Programming Issues –Dangling pointers –Memory Leaks

Which manages memory?

Rule of thumb: If a class manages memory allocation, it should also handle memory deallocation (letting a class allocate space for its data member and another free the space is error prone)

Programming issues Once the decision on where (library or application) the memory is to be manage is decide, there are two programming issues: Dangling pointers Memory leaks

Dangling pointers Cause: Variable is pointing to a memory space that has been freed. Consequence: Programmer refers to variable thinking it points to valid memory location  Segmentation fault. When: Library does not manage memory properly, and application ends up with dangling pointers.

Class X { // constructor and // destructor of X // not needed int X::f(LibClass a) { return a.get_int(); } Example with dangling pointer Class LibClass { int *p; pulbic: LibClass::LibClass(int i) { p = new (int *); p = i; } LibClass::~LibClass(){ delete p; } int LibClass::get_int(){ return *p; } void LibClass::set_int(int i){ *p = i; } void main () { int i=1; LibClass b(i); X c; c.f(b); b.set_int(2); } Implicit call to copy constructor Destructor called to destroyed copy created by copy constructor Space for p data member of b has been destroyed  Dangling pointer

Solution to dangling pointers Need to introduce an copy constructor LibClass::LibClass( &LibClass ) Options: –Copy constructor creates its own space and then copy the values. –Copy constructor increment counter, destructor decrement counter and destructor free memory when counter = 0.

Questions How should the example be modified to implement each option of the solution? What are the pros and cons of each option?

Memory Leaks Cause: At one point of execution, no variables point to a memory space allocated by the program. Consequence: memory memory gets full and space cannot by allocated When: Neither library nor application frees memory

Example of memory leak Class LibClass { int *p; pulbic: LibClass::LibClass(int *i) { p = new (int *); p = i; } LibClass::~LibClass() { } int LibClass::get_int() { return *p; } void LibClass::set_int(int i){ *p = i; } void main () { int i = 1; int j = 2; LibClass b(i); LibClass c(j); c = b; } Destructor does not free memory Implict call to operator=(&LibClass) Problem of memory leak: After assignment ‘c = b’, the space to which b.p was pointing has not been freed, and no variables point to it.

Solution to memory leaks Need to introduce an explicit = operator LibClass::operator= ( &LibClass ) Options: –the = operator creates its own space and copy values into space and the destructor frees space (problem: keep consistency between copies) –the = operator increment a counter and destructor decrements counter and frees space when counter = 0

Questions How should the example be modified to implement each option of the solution? What are the pros and cons of each option? If ‘main’ was to handle memory management, how would you modify the implementation?