180 likes | 355 Views
ITEC 320. Lecture 11 Pointers(1). Review. Packages Generic Child Homework 3 is posted. Outline. Pointers What? Why? Relationship to Java Examples. Visualization. Your iPhone is waiting for pickup at 123 Computer Lane. Your pickup code is 1234567890. versus.
E N D
ITEC 320 Lecture 11 Pointers(1)
Review • Packages • Generic • Child • Homework 3 is posted
Outline • Pointers • What? • Why? • Relationship to Java • Examples
Visualization Your iPhone is waiting for pickup at 123 Computer Lane. Your pickup code is 1234567890. versus
TechnicalDefinition • A pointer contains the memory address of where a particular value is stored • A variable contains the particular value • A reference allows access to a particular value at a particular memory address • Issues • How to access the value (de-referencing) • Typing • Relationship to hardware
Currently class Box { intl; int w; } class Client { public static void main() { Box p; -- A local variable p = new Box(); p.l:= 3; p.w := 4; System.out.println(p.l + p.w); Box q = null; -- What if no initialization? System.out.println (q.l); -- What happens here? } }
Ada procedureboxexampleistype Box isrecord l, w: Integer := 0;end record; b1, b2: Box;begin b1.l := 11; b1.w := 22; b2 := (33, 44);if b1 = b2 thenput_line("Same");elseput_line("Different");end if;endboxexample;
Ada procedure boxptrexample1 istype Box isrecord l, w: Integer := 0;end record; -- A type for pointers to boxestypeBoxPtrisaccess Box; b1: Box; p1: BoxPtr;begin b1 := (11, 22); p1 := new Box; -- can do new Box’(1,2) as well p1.all := (33, 44);if b1 = p1.allthenput_line("Same");elseput_line("Different");end if; end boxptrexample1
Deallocation • New creates memory • What happens when you are finished with it? • Call the dispose () procedure (make sure var deleting is in out or local) • Have to create it unfortunately with Ada.Unchecked_Deallocation; procedure dispose isnewAda.Unchecked_Deallocation( Object => Box, -- The type of object being deallocated Name =>BoxPtr-- The type of pointer being followed );
Comparisons • Similarities • Both refer to values allocated with new • Strongly typed • NULL points to invalid data • Refers to addresses • Create garbage • Implicitly dereference
Differences • Separate types for pointer and values • Can access both pointer and value • Point to stack and heap • Pointer arithmetic (requires type checking to be turned off) • No automatic garbage collection • Dangling references • Implicit / Explicit dereferencing
Mixing • To set a pointer to regular data requires changes with ada.text_io; use ada.text_io; with ada.integer_text_io; use ada.integer_text_io; procedure pointer is type IP is access all Integer; ref: IP; x : aliased Integer := 5; begin ref := x'Access; put(ref.all); end pointer; Power user mode
Rationale • Why do you think languages have pointers / references? • Memory management • Compile time benefits • You know what size references are… • Class a reference points to can be any size
Error What is wrong with this? type Box is ... type BoxPtr is access Box; b1: Box := (2, 3); p1: BoxPtr; begin p1.all := b1; put(p1.all.w);
Memory Management • Java • How does it work? • Ada • Explicitly create and release memory • Speed concerns • Ways around it
Memory Leak Java Foo f; f = new Foo(); f = new Foo(); Ada declare f: Foo; begin f = new Foo; f = new Foo; Why is this normal Java? How do you fix this?
Problems • Dereference a NULL pointer • Aliases • 2 pointers pointing to same data • Dangling pointer • Pointing to something that has been disposed • Creating garbage / memory link
Review • Pointers • Next time • Linked list of integers • Updating linked list to be generic