1 / 18

ITEC 320

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.

haven
Télécharger la présentation

ITEC 320

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. ITEC 320 Lecture 11 Pointers(1)

  2. Review • Packages • Generic • Child • Homework 3 is posted

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

  4. Visualization Your iPhone is waiting for pickup at 123 Computer Lane. Your pickup code is 1234567890. versus

  5. 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

  6. 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? } }

  7. 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;

  8. 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

  9. 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    );

  10. Comparisons • Similarities • Both refer to values allocated with new • Strongly typed • NULL points to invalid data • Refers to addresses • Create garbage • Implicitly dereference

  11. 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

  12. 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. 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. 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);

  15. Memory Management • Java • How does it work? • Ada • Explicitly create and release memory • Speed concerns • Ways around it

  16. 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?

  17. 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. Review • Pointers • Next time • Linked list of integers • Updating linked list to be generic

More Related