1 / 20

Implications of Substitution

Implications of Substitution. CMPS 2143. Overview. Memory Layout and methods of space allocation Assignment Copies and Clones Equality and Identity. Inheritance and Substitution. Subtle, yet pervasive impact on MANY aspects of a programming language Type system

waite
Download Presentation

Implications of Substitution

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Implications of Substitution CMPS 2143

  2. Overview • Memory Layout and methods of space allocation • Assignment • Copies and Clones • Equality and Identity

  3. Inheritance and Substitution • Subtle, yet pervasive impact on MANY aspects of a programming language • Type system • Meaning of assignment, testing for equivalence • Creation of copies • Storage allocation • Is-a relationship: associate a type (of a variable) with a set of values • Window win = new Window(); • Window win = new TextWindow();

  4. Storage allocation • Commonly believed variables allocated on the run-time stack (in activation records) are more efficient than variables allocated on the heap. • Language designers go to great lengths to make this happen • Major problem: storage requirements must be determined at statically (at compile-time) or at least at procedure-entry time. • Values the variables hold are determined at runtime.

  5. Subclasses and storage requirements • Subclasses may have more data (not present in superclass) • Ex. textWindow may have more data than window, • eg. a string and cursorLocation • When allocating for a Window object, should we really allocate for a TextWindow object, just in case?

  6. THREE SOLUTIONS • Minimum space allocation: Allocate the amount of space necessary for the base class. • Maximum space allocation: Allocate the maximum amount of space necessary to hold any legal value (from any of the classes in the hierarchy) • Dynamic memory allocation: Allocate only the amount of space necessary to hold a single pointer/reference. (Allocate the space necessary for the value at runtime on the heap.)

  7. Minimum space allocation • C was designed to be efficient, C++ retains many concepts of nondynamic and dynamic variables Window win; //puts an object on the stack Window * tWinPtr; //puts an pointer on the stack tWinPtr = new TextWindow(); //allocates on the heap • What happens if …? win = *tWinPtr; • Space allocated to win on the stack is not large enough!!!

  8. Minimum Space Allocation • Default: not all fields copied/only the corresponding fields. (termed slicing) • C++ can override the meaning of assignment, but how without losing some information? • Do we care? • Can only call methods in win (Window) • What if methods are overridden in the subclass and they access extra data?

  9. Minimum Space Allocation • C++: 2 solutions • For pointers (and references) – member function selected is determined by dynamic value of receiver • For other variables – binding determined by static class (the class at declaration) • Given this modified rule – not possible for a method to access fields that are physically present in memory in the object

  10. Maximum Space Allocation • Might seem to be ideal (although possibly wasteful) • Size of object must be known by scanning ENTIRE program • Need 2 pass compiler • Need to see all libraries, linked in code, etc. • No major programming language uses this approach

  11. Dynamic Memory Allocation • Values of objects not stored on the stack – only the pointer or reference is • Values stored in the heap. • Java, Smalltalk, Object Pascal, Objective-C use this approach • Must use new to allocate space in the heap. • Will have consequences with assignment semantics!!!

  12. Garbage collection • When data stored in heap, must be recovered • Use delete in C++ • Use auto garbage collector

  13. Assignment – 2 interpretations • Copy semantics – assignment copies entire value of right side, assigning it to left, thereafter the two values are independent (changes in one not reflected in other) • Sometimes used in C++, sometimes not • Pointer semantics – Assignment changes the reference of left side to be of right side (aliasing). Two pointers refer to the same memory location. (Changes in one, will affect the other.) • Java, CLOS, Object Pascal • If used, languages usually provide a means for true copy

  14. Copies and Clones • How to copy a value that references other objects? • Shallow copy • assignment of references • Deep copy • Overload assignment • Write copy constructor • Overload clone method

  15. Copy constructors • Define what you want to copy • Argument is a reference parameter of same type public class MyClass { public: MyClass(constMyClass source) { ….}; • Considered GOOD practice to ALWAYS include a copy constructor

  16. Cloning in Java • Implement Cloneable interface • Override method clone class PlayingCard implements Cloneable { : public Object clone () throws CloneNotSupportedException { Object newCard = super.clone); return newCard; } } • Default behavior creates a shallow copy. Add additional code to create a deep copy

  17. Equality • Identity – are two objects the same entity • Equality – two objects that contain the same values char * a = “abc”; char * b = “abc”; If (a==b) // T/F?

  18. Equality Testing • Override == • Override or write equals method • There is an equals method in Java’s Object class

  19. Paradoxes of equality testing • Equality can be done at various levels • Let p1 and p2 be parent classes and c1 and c2 be child classes Is p1.equals(c1) same as c1.equals(p1) ? If p1.equals(c1) and p1.equals(c2), does c1.equals(c2)? If p1.equals(c1) and c1.equals(p2), does p1.equals(p2)? • HMMM?

  20. Study questions • Pg. 286: 2-5, 9

More Related