Implications of Substitution Fall 2005 OOPD John Anthony.

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Introduction to Programming with Java, for Beginners “Has a” Relationship.
Informática II Prof. Dr. Gustavo Patiño MJ
Introduction to Programming with Java, for Beginners
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.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
Run-time Environment and Program Organization
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Ch 4. Memory Management Timothy Budd Oregon State University.
OOP Languages: Java vs C++
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Comparison of OO Programming Languages © Jason Voegele, 2003.
Programming Languages and Paradigms Object-Oriented Programming.
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Runtime Environments Compiler Construction Chapter 7.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Interacting Classes Lecture #6 CMPSCI 121, Spring 2005 Introduction to Problem Solving with Computers Prof. McCallum No quiz today. It will again be in.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic memory allocation and Pointers Lecture 4.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
More C++ Features True object initialisation
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Session 22 Chapter 11: Implications of Inheritance.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Programming Languages and Paradigms Activation Records in Java.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Lecture 12 Implementation Issues with Polymorphism.
ISBN Chapter 12 Support for Object-Oriented Programming.
A First Book of C++ Chapter 12 Extending Your Classes.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Design issues for Object-Oriented Languages
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Chapter 5: Enhancing Classes
Pointers and Dynamic Variables
Dynamically Allocated Memory
CSC 253 Lecture 8.
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
PZ09A - Activation records
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Java Programming Language
Pointers, Dynamic Data, and Reference Types
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Run-time environments
Presentation transcript:

Implications of Substitution Fall 2005 OOPD John Anthony

Topics Memory Allocation Strategies Memory Allocation Strategies The Java Memory Allocation Model The Java Memory Allocation Model A Java Stack and Heap Example A Java Stack and Heap Example Memory Allocation and Inheritance Memory Allocation and Inheritance An Example in C++ Using the Stack An Example in C++ Using the Stack Reference Assignment Strategies Reference Assignment Strategies

Memory Allocation Generally broken down into two strategies (although variance does exist).

Static (Stack-based) Allocation Static allocation means allocation of memory before the program starts and retention until the end. Static allocation means allocation of memory before the program starts and retention until the end.allocation Compiler determines the exact size of the data structure based on the software text. Compiler determines the exact size of the data structure based on the software text. Memory allocation and release is determined at procedure entry and exit. Memory allocation and release is determined at procedure entry and exit. This means that the size of the program and its “data” are determined before execution. This means that the size of the program and its “data” are determined before execution.

Dynamic (Heap-based) Allocation Dynamic allocation means allocation of memory during program execution. Dynamic allocation means allocation of memory during program execution.allocation Compiler cannot determine the exact size of the data structure based on the software text. Compiler cannot determine the exact size of the data structure based on the software text. Memory allocation and release is not determined at procedure entry and exit. Memory allocation and release is not determined at procedure entry and exit. Memory release either handled by programmer or system (garbage collection). Memory release either handled by programmer or system (garbage collection).

Java (Thread) Execution State Execution state of a Java thread has several runtime data areas: Java stack succession of frames representing method calls Object heap Objects used by a thread Method area Classes used by a thread

The Java Stack A new frame for each method execution A new frame for each method execution A frame includes local variables of the associated method and operand stack that contains partial results (operands) A frame includes local variables of the associated method and operand stack that contains partial results (operands) A frame also contains registers such as the program counter A frame also contains registers such as the program counter

Stack, Heap, and Method Area The heap associated with a thread contains all the objects used by the thread (objects accessible from the thread’s Java stack) The heap associated with a thread contains all the objects used by the thread (objects accessible from the thread’s Java stack) The Class Pool associated with a thread contains the classes used by the thread. The Class Pool associated with a thread contains the classes used by the thread. Class Pool Stack Object heap Class Reference Object Reference

Stack – A Closer Look Java Code Stack > int x; > x = 5; > double min = 0.5; > boolean done = false; Fernando Pereira University of Pennsylvania

Stack & Heap – A Closer Look Java Code Stack and Heap > int x = 99; > Counter c1; > c1 null > c1 = new Counter(); > c1 > c1.incrementCount(); > Counter c2 = new Counter(); > c2 Fernando Pereira University of Pennsylvania

Value of a Reference Variable The value of are reference variable is either null or a “heap address” The value of are reference variable is either null or a “heap address” Example: Example: > Counter c1; > Counter c1; > c1 > c1 null null > c1 = new Counter(); > c1 = new Counter(); > c1 > c1 e05ad6 is a hexadecimal (base 16) number e05ad6 is a hexadecimal (base 16) number We don’t have to (and can’t) deal with these hex numbers directly We don’t have to (and can’t) deal with these hex numbers directly Fernando Pereira University of Pennsylvania

“Has a” Relationship We’ll look at an example where an object A has an instance variable which is an object whose type is B. (A “has a” B.) We’ll look at an example where an object A has an instance variable which is an object whose type is B. (A “has a” B.) We will create a DormRoom object, and a Freshman object whose room is that DormRoom We will create a DormRoom object, and a Freshman object whose room is that DormRoom What code should we write? What will the stack and the heap will look like?

public class DormRoom{ private int num; private String bldgName; public DormRoom(int n, String b){ num = n; bldgName = b; } public String getLocation(){ return num + " " + bldgName; } } > DormRoom room = new DormRoom(208, "Hill"); > room.getLocation() "208 Hill" DormRoom Code and UML

A DormRoom on the Heap > DormRoom room = new DormRoom(208, "Hill"); > room.getLocation() "208 Hill"

public class Freshman{ private String name; private DormRoom room; public Freshman(String n, DormRoom r){ name = n; room = r; } public String getName(){ return name;} public DormRoom getRoom(){ return room;} } > DormRoom room = new DormRoom(208, "Hill"); > Freshman f = new Freshman("jo", room); > f.getName() "jo" > f.getRoom().getLocation() "208 Hill" Freshman Code and UML

A Freshman on the Heap :) > DormRoom room = new DormRoom(208, "Hill"); > Freshman f = new Freshman("jo", room); > f.getName() "jo" > f.getRoom().getLocation() "208 Hill"

Memory Allocation and Inheritance If Stack Allocation is a more efficient memory allocation and reclamation strategy, then why use the heap? If Stack Allocation is a more efficient memory allocation and reclamation strategy, then why use the heap? Let’s look at one challenge as seen through the eyes of C++ and polymorphism. Let’s look at one challenge as seen through the eyes of C++ and polymorphism.

Consider the following…. class Window { public: virtual void oops(); private: int height; int width; }; class TextWindow : public Window { public: virtual void oops(); private: char * contents; int cursorLocation; }; If we create a new Window (Window win;), how much space on the Stack should be allocated?

Three Choices Allocate enough memory for the base class only. (C++) Allocate enough memory for the base class only. (C++) Allocate the maximum memory needed for any legal value (base or subclass). Allocate the maximum memory needed for any legal value (base or subclass). Allocate enough space to only hold a pointer. (Smalltalk, Java) Allocate enough space to only hold a pointer. (Smalltalk, Java)

Minimum Static Space Allocation C++ uses this approach. C++ uses this approach. What happens when we execute the following code: What happens when we execute the following code: Window win; Window *tWinPtr;... tWinPtr = new TextWindow... Win = *tWinPtr

Slicing height width … height width … contents cursorLocation … void Window::oops() { cout << “Window oops”” << endl; } Void Window::oops() { cout << “TextWindow oops”” << cursorLocation << endl; }

Rules for Member Function Binding in C++ Variables declared as references or pointers, the binding of the function name to the function body is based on the dynamic class (as you’d perhaps expect). Variables declared as references or pointers, the binding of the function name to the function body is based on the dynamic class (as you’d perhaps expect). With variables that are not pointers (i.e. declared on the stack), the binding of the function name to the function body is based on the static class. With variables that are not pointers (i.e. declared on the stack), the binding of the function name to the function body is based on the static class.

Example Window win; TextWindow *tWinPtr, *tWin;... tWinPtr= new Textwindow; win = * tWinPtr; tWin = tWinPtr; Win.oops();tWin.oops();

Maximum Static space Allocation Eliminates slicing problem by allocating the maximum amount of memory necessary. Eliminates slicing problem by allocating the maximum amount of memory necessary. In order to do this, the entire program needs to be scanned. In order to do this, the entire program needs to be scanned. What about dynamic class loading….? What about dynamic class loading….? No major OO language takes this approach due to restriction of “program scan”. No major OO language takes this approach due to restriction of “program scan”.

Dynamic Memory Allocation The value is stored in a separate data area called the Heap. The value is stored in a separate data area called the Heap. Fixed size pointers are stored on the Stack and point to the heap. Fixed size pointers are stored on the Stack and point to the heap. Space on the heap is allocated when the object is created via the “new” keyword (for Java). Space on the heap is allocated when the object is created via the “new” keyword (for Java). What happens when after executing an assignment statement? What happens when after executing an assignment statement?

Pointer Assignment Counter c1 = new Counter(); Counter c2 = new Counter(); c2 = c1; c1 c2 0 0 Stack Heap Counter

Copy Assignment Counter c1 = new Counter(); Counter c2 = new Counter(); c2 = c1; c1 c2 0 0 Stack Heap Counter 0 Copied as a result of assignment

A Hybrid Approach c1 c2 0 0 Stack Heap Counter 0 Copied as a result of modification (not assignment) Counter c1 = new Counter(); Counter c2 = new Counter(); c2 = c1;