Program Realisation 2 Today’s Topics http://www.win.tue.nl/˜hemerik/2IP20/ Lecture 5 • Inheritance revisited Kees Hemerik • Dynamic variables : heap, pointer type ˆT , New , Dispose Tom Verhoe ff Informatics Thriller: My Life among the Pointers Technische Universiteit Eindhoven Faculteit Wiskunde en Informatica • Linear dynamic data structures with pointers: Linked Lists Software Engineering & Technology Feedback to T.Verhoeff@TUE.NL � 2007, T. Verhoe ff @ TUE.NL c 1 Program Realization 2: Lecture 5 � 2007, T. Verhoe ff @ TUE.NL c 2 Program Realization 2: Lecture 5 Inheritance Revisited Dynamic Method Binding: Advanced Example Inheritance is a mechanism with many faces. We use it only for: 1 type 2 TParent = class (TObject) procedure M; virtual ; { possibly abstract; } 3 4 procedure D; virtual ; • Multiple co-existing implementations of an ADT end ; { class TParent } 5 6 Parent : no fields; methods are virtual , usually also abstract 7 TChild = class (TParent) procedure M; override ; 8 Child : same contract; private fields; override the methods 9 end ; { class TChild } 10 11 procedure TParent.M; begin Write(’TParent.M ’) end ; { omit if abstract } • Subtyping (specialization; substitutability principle; e.g. VCL) 12 13 procedure TParent.D; begin M ; M end ; Parent : usually has private fields and virtual methods 14 15 procedure TChild.M; begin Write(’TChild.M ’) end ; Child : stronger contract: weaker pre, stronger post; 16 17 begin may add fields, add/ override methods with TChild.Create do D { outputs: ’TChild.M TChild.M ’ } 18 � 2007, T. Verhoe ff @ TUE.NL c 3 Program Realization 2: Lecture 5 � 2007, T. Verhoe ff @ TUE.NL c 4 Program Realization 2: Lecture 5
Inheritance: Create, Destroy Inheritance Advice 1 type TChild = class (TParent) ... end ; 2 3 4 constructor TChild.Create; Reasoning about inheritance: begin 5 inherited Create { takes care of fields in TParent } 6 ; { create/initialize fields specific to TChild } 7 • In terms of the contracts end ; { Create } 8 9 10 destructor TChild.Destroy; • Not in terms of “how it works” begin 11 { free fields specific to TChild } 12 ; inherited Destroy { takes care of fields in TParent } 13 end ; { Destroy } 14 � 2007, T. Verhoe ff @ TUE.NL c 5 Program Realization 2: Lecture 5 � 2007, T. Verhoe ff @ TUE.NL c 6 Program Realization 2: Lecture 5 Static Memory Allocation and Deallocation Dynamic Memory Allocation and Deallocation 1 procedure UseA; 1 var var A : VeryBigTypeA; 2 p : ˆ VeryBigTypeA; { pointer } 2 begin { A allocated } 3 q : ˆ VeryBigTypeB; { pointer } 3 1 var ’Use A’ 4 4 A : VeryBigTypeA; 2 end ; { A deallocated } 5 { p, q allocated } 5 begin B : VeryBigTypeB; 3 6 New(p) { pˆ allocated } 6 4 7 procedure UseB; 7 ; ’Use pˆ’ 5 begin { A, B allocated } var B : VeryBigTypeB; 8 8 ; Dispose(p) { pˆ deallocated } ’Use A’ 6 begin { B allocated } 9 9 7 ; ’Use B’ ’Use B’ 10 10 ; New(q) { qˆ allocated } 8 end { A, B deallocated } end ; { B deallocated } 11 11 ; ’Use qˆ’ 12 12 ; Dispose(q) { qˆ deallocated } 13 begin UseA ; UseB end { p, q deallocated } 13 end � 2007, T. Verhoe ff @ TUE.NL c 7 Program Realization 2: Lecture 5 � 2007, T. Verhoe ff @ TUE.NL c 8 Program Realization 2: Lecture 5
Memory Organization: Stack and Heap Pascal Pointer Type type PT = ˆT; { pointer type for type T, T must be a type name } Dynamic variable xˆ H E Gaps possible A P Set of values for ˆT : pointers to dynamic variables of type T , Dynamic variable xˆ.tˆ including the special pointer nil , pointing to nothing ❄ Unused Dynamic variables exist on the heap , which grows toward the stack. ✻ S Memory management of the heap is up to the program (not the Local variables Mb T compiler). Local variables Ma A C Global variables K N.B. Static variables exist on the stack, managed by the compiler, based on block invocations. � 2007, T. Verhoe ff @ TUE.NL c 9 Program Realization 2: Lecture 5 � 2007, T. Verhoe ff @ TUE.NL c 10 Program Realization 2: Lecture 5 Pascal Pointer Type Operations Costs of Pointers Operations on ˆT ( p, q expressions of type ˆT ) Memory usage for var p: ˆT : • pˆ (dereferencing) : pre : p <> nil • p = nil : O (1) , generally 4 bytes ret : the variable of type T pointed to by p • p <> nil : O (1) + memory usage for pˆ (except when sharing ) • p := q (assignment): pre : q = Q post : p = Q (N.B. aliasing!) Speed of operations: • New(p) : pre : true; post : p points to newly allocated, uninitialized dynamic variable of type T • nil , p = q , p := q : O (1) • Dispose(p) : pre : p = P /\ P <> nil • New(p) , Dispose(p) : depends on heap manager post : p is undefined, variable Pˆ is deallocated � 2007, T. Verhoe ff @ TUE.NL c 11 Program Realization 2: Lecture 5 � 2007, T. Verhoe ff @ TUE.NL c 12 Program Realization 2: Lecture 5
Embedding and Nesting with Pointers Recursion with Pointers: Linked List 1 var 5 NodeP = ˆNode; { pointer to a node } (*ADDED*) 6 Node = record { node in linked list of windows } (*ADDED*) p: ˆT { plain pointer } 2 7 xl, yl, xh, yh: Integer; { coordinates of window } (*ADDED*) a: array [ Char ] of ˆT { embedded pointers } 3 tail: NodeP; { pointer to next window, if not nil } (*ADDED*) 8 4 9 end ; (*ADDED*) 5 type R = record p: ˆT end ; { embedded pointer } 6 ✲ 200, 0, 400, 100 ✲ 200, 0, 400, 100 w w 7 r r 8 var ❄ ❄ q: ˆR { nested pointers } 9 u ✲ u 100, 50, 300, 150 100, 50, 300, 150 ❆ ❆ 10 ❆ r r ❆ 11 begin ❆ ❄ ❆ ❆ ❄ ... pˆ ... { is of type T } 12 ❆ v ✲ v ✲ ❆ ❯ 0, 100, 200, 200 0, 100, 200, 200 ... a[’c’]ˆ ... { is of type T } 13 r r ... qˆ.pˆ ... { is of type T } 14 ❄ ❄ � 2007, T. Verhoe ff @ TUE.NL c 13 Program Realization 2: Lecture 5 � 2007, T. Verhoe ff @ TUE.NL c 14 Program Realization 2: Lecture 5 Traversing a Linked List Find Last Node of a Non-Empty Linked List 1 function Count(p: NodeP): Integer; 1 function FindLast(p: NodeP): NodeP; // pre: p points to nil-terminated tail-linked list // pre: p <> nil points to nil-terminated tail-linked list 2 2 // ret: length of list pointed to by p // ret: pointer to last element in list 3 3 begin begin 4 4 Result := 0 Result := p 5 5 6 6 // inv: Result + length of list(p) = length of (initial p) { inv: Result <> nil } 7 7 ; while p <> nil do begin ; while Resultˆ.tail <> nil do begin 8 8 Result := Result + 1 Result := Resultˆ.tail 9 9 ; p := pˆ.tail 10 10 end end { Resultˆ.tail = nil } 11 11 12 12 end ; { Count } end ; { FindLast } 13 13 � 2007, T. Verhoe ff @ TUE.NL c 15 Program Realization 2: Lecture 5 � 2007, T. Verhoe ff @ TUE.NL c 16 Program Realization 2: Lecture 5
Bounded Linear Search on a Linked List ‘Cons’ an New Element to a Linked List 1 function Find(p: NodeP; ...): NodeP; // pre: p points to nil-terminated tail-linked list 2 1 procedure Cons( var p: NodeP; const v: TValue); // ret: pointer to first element in list satisfying ..., 3 // pre: p points to nil-terminated tail-linked list 2 // if such an element occurs in the list, else nil 4 // post: p points to list with v added in front 3 begin 5 var 4 Result := nil 6 q: NodeP; { to create new node } 5 7 ; while p <> Result do begin { p <> nil } begin 8 6 if ... pˆ ... then { pˆ satisfies search condition } New(q) 9 7 Result := p 10 ; qˆ.value := v 8 11 else ; qˆ.tail := p 9 p := pˆ.tail 12 ; p := q 10 13 end end ; { Cons } 11 14 end ; { Find } 15 � 2007, T. Verhoe ff @ TUE.NL c 17 Program Realization 2: Lecture 5 � 2007, T. Verhoe ff @ TUE.NL c 18 Program Realization 2: Lecture 5 ‘Cons’ an New Element to a Linked List (2) Delete First Element from a Linked List 1 procedure Cons( var p: NodeP; const v: TValue); 1 procedure DeleteFirst( var p: NodeP); // pre: p points to nil-terminated tail-linked list // pre: p <> nil points to nil-terminated tail-linked list 2 2 // post: p points to list with v added in front // post: p points to list minus first element 3 3 var var 4 4 q: NodeP; { to save initial value of p } q: NodeP; { to save pˆ.tail } 5 5 begin begin 6 6 q := p q := pˆ.tail (* p := pˆ.tail may cause memory leak *) 7 7 ; New(p) { if necessary, dispose pˆ.value here } 8 8 ; pˆ.value := v ; Dispose(p) (* can’t do this first, because then p undefined 9 9 ; pˆ.tail := q ; p := q 10 10 end ; { Cons } end ; { DeleteFirst } 11 11 � 2007, T. Verhoe ff @ TUE.NL c 19 Program Realization 2: Lecture 5 � 2007, T. Verhoe ff @ TUE.NL c 20 Program Realization 2: Lecture 5
Recommend
More recommend