Pointers, Polymorphism, and Memory Allocation

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types
Advertisements

Run-Time Storage Organization
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
OOP Languages: Java vs C++
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Basic Semantics Associating meaning with language entities.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
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.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students 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.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
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.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Memory Management.
Constructors and Destructors
Object Lifetime and Pointers
Dynamic Storage Allocation
Pointers and Dynamic Arrays
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9 More on Objects and Classes
CS Data Structures Chapter 8 Lists Mehmet H Gunes
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
COMP 2710 Software Construction Pointers
Pointers Revisited What is variable address, name, value?
Object-Oriented Programming Using C++
Dynamic Memory CSCE 121 J. Michael Moore.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
This pointer, Dynamic memory allocation, Constructors and Destructor
Object Oriented Programming
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9 Classes: A Deeper Look, Part 1
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
Constant pointers and pointers to constants
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 12 Pointers and Memory Management
Constructors and Destructors
Binding Times Binding is an association between two things Examples:
9-10 Classes: A Deeper Look.
Dynamic Memory.
Dynamic Memory Allocation
Data Structures and Algorithms Memory allocation and Dynamic Array
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
9-10 Classes: A Deeper Look.
Classes and Objects Object Creation
Run-time environments
SPL – PS2 C++ Memory Handling.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Pointers, Polymorphism, and Memory Allocation C++ Interlude 2 © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Memory Allocation for Variables and Early Binding of Methods Declare variable x to have data type int C++ compiler allocates memory cell to hold an integer Use the identifier x to refer to this cell A function’s locally declared variables Placed into an activation record with parameters and bookkeeping data Activation record placed on run-time stack Activation record destroyed when function finished © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Memory Allocation for Variables and Early Binding of Methods Storage for data members of an object Also placed into an activation record. Data fields placed on the run-time stack just as primitive data types are. This is early binding , made during compilation Cannot be altered during execution © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Memory Allocation for Variables and Early Binding of Methods Automatic memory management and early binding sometimes insufficient Need to take advantage of polymorphism. Must access an object outside of the function or method that creates it. © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

© 2017 Pearson Education, Hoboken, NJ. All rights reserved Problem to Solve Need to write a function – takes two arguments: An object of any of the three types of boxes (from Interlude 1) An item of type string Function should place item in box by invoking box’s setItem method © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Problem to Solve FIGURE C2-1 UML class diagram for a family of classes © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

© 2017 Pearson Education, Hoboken, NJ. All rights reserved Problem to Solve You may think this function would suffice © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

© 2017 Pearson Education, Hoboken, NJ. All rights reserved Problem to Solve Used in this context Code compiles, but does not perform as you would expect due to © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

© 2017 Pearson Education, Hoboken, NJ. All rights reserved Problem to Solve Version of setItem called is determined when the program is compiled. Need a way to communicate to compiler Code to execute should not be determined until program is running. Called late binding © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Pointers and Program’s Free Store FIGURE C2-2 Sample program memory layout © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Pointers and Program’s Free Store FIGURE C2-3 Run-time stack and free store after myboxPtr points to a MagicBox object and its data member item is set © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Pointers and Program’s Free Store FIGURE C2-4 myBoxPtr and the object to which it points © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Pointers and Program’s Free Store FIGURE C2-5 Two pointer variables that point to the same object © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

© 2017 Pearson Education, Hoboken, NJ. All rights reserved Deallocating Memory When memory to which pointer variable points is no longer needed Deallocate it by using delete operator. Then set pointer variable to nullptr Otherwise dangling pointer exists It would still contain address of object that was deallocated. Can be source of serious errors. © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

© 2017 Pearson Education, Hoboken, NJ. All rights reserved Avoiding Memory Leaks Memory leaks occur when An object has been created in the free store, but Program no longer has a way to access LISTING C2-1 Poorly written function that allocates memory in the free store © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Avoiding Memory Leaks Figure C2-6 An assignment that causes an inaccessible object. To prevent memory leak, do not use a function to return a pointer to a newly created object © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Avoiding Memory Leaks LISTING C2-2 Header file for the class GoodMemory © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Avoiding Memory Leaks LISTING C2-3 Implementation file for the class GoodMemory © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Avoiding Dangling Pointers Situations that can cause dangling pointer if you do not set a pointer variable to nullptr after using delete If you declare a pointer variable but do not assign it a value © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Avoiding Dangling Pointers FIGURE C2-7 Two pointers referencing (pointing to) the same object © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Avoiding Dangling Pointers FIGURE C2-8 Example of a dangling pointer © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Virtual Methods and Polymorphism Allow compiler to perform the late binding necessary for polymorphism Declare methods in base class as virtual. LISTING C2-4 Revised header file for the class PlainBox © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Virtual Methods and Polymorphism LISTING C2-4 Revised header file for the class PlainBox © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Virtual Methods and Polymorphism LISTING C2-4 Revised header file for the class PlainBox © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Virtual Methods and Polymorphism To fully implement late binding, create variables in free store and use pointers to reference them © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Dynamic Allocation of Arrays An ordinary C++ array is statically allocated Can use new operator to allocate an array dynamically © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Dynamic Allocation of Arrays delete returns a dynamically allocated array to system for reuse Increase size of dynamically allocated array © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

A Resizable Array-Based Bag Use a resizable array to implement ADT bag so that bag never becomes full © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

A Resizable Array-Based Bag Use a resizable array to implement ADT bag so that bag never becomes full © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

© 2017 Pearson Education, Hoboken, NJ. All rights reserved End Interlude 2 © 2017 Pearson Education, Hoboken, NJ.  All rights reserved