Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 320 Lecture 11 Pointers(1). Pointers Review Packages –Generic –Child Homework 3 is posted.

Similar presentations


Presentation on theme: "ITEC 320 Lecture 11 Pointers(1). Pointers Review Packages –Generic –Child Homework 3 is posted."— Presentation transcript:

1 ITEC 320 Lecture 11 Pointers(1)

2 Pointers Review Packages –Generic –Child Homework 3 is posted

3 Pointers Outline Pointers What? Why? Relationship to Java Examples

4 Pointers Visualizatio n versus Your iPhone is waiting for pickup at 123 Computer Lane. Your pickup code is 1234567890.

5 Pointers Technical Definition 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

6 Pointers Currently class Box { int l; 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? }

7 Pointers Ada procedure boxexample is type Box is record l, w: Integer := 0; end record; b1, b2: Box; begin b1.l := 11; b1.w := 22; b2 := (33, 44); if b1 = b2 then put_line("Same"); else put_line("Different"); end if; end boxexample;

8 Pointers Ada procedure boxptrexample1 is type Box is record l, w: Integer := 0; end record; -- A type for pointers to boxes type BoxPtr is access 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.all then put_line("Same"); else put_line("Different"); end if; end boxptrexample1

9 Pointers 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 is new Ada.Unchecked_Deallocation( Object => Box, -- The type of object being deallocated Name => BoxPtr -- The type of pointer being followed );

10 Pointers Comparison s Similarities –Both refer to values allocated with new –Strongly typed –NULL points to invalid data –Refers to addresses –Create garbage –Implicitly dereference

11 Pointers Difference s 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

12 Pointers 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

13 Pointers 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

14 Pointers Error type Box is... type BoxPtr is access Box; b1: Box := (2, 3); p1: BoxPtr; begin p1.all := b1; put(p1.all.w); What is wrong with this?

15 Pointers Memory Managemen t Java –How does it work? Ada –Explicitly create and release memory –Speed concerns –Ways around it

16 Pointers Memory Leak Java Foo f; f = new Foo(); Ada declare f: Foo; begin f = new Foo; How do you fix this? Why is this normal Java?

17 Pointers 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

18 Pointers Review Pointers Next time –Linked list of integers –Updating linked list to be generic


Download ppt "ITEC 320 Lecture 11 Pointers(1). Pointers Review Packages –Generic –Child Homework 3 is posted."

Similar presentations


Ads by Google