1 / 29

Programming Language Concepts (CIS 635)

Programming Language Concepts (CIS 635). Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635. 0.4. 7.2. 7.2. 0.4. 0.4. Problems with Pointers. Dangling Pointer A: Delete A B: Garbage (lost heap-dynamic variables) A: A: B: B:.

hazel
Télécharger la présentation

Programming Language Concepts (CIS 635)

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. Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635

  2. 0.4 7.2 7.2 0.4 0.4 Problems with Pointers • Dangling Pointer A: Delete A B: • Garbage (lost heap-dynamic variables) A: A: B: B:

  3. Ways to Create Dangling Pointers int * A, B; A = new int; A = 5; B = A; delete A; /* B is still pointing to the address of object A returned to stack */

  4. Ways to Create Dangling Pointers int * A; int * sub () { int B; B = 5; return B;} main () { A = sub(); . . . } /* A has been assigned the address of an object that is out of scope */

  5. Arrays • Ordered sequence of fixed number of objects all of the same type • Indexed by integer, subrange, or enumeration type, called subscript • Multidimensional arrays have one subscript per each dimension • L-value for array element given by accessing formula

  6. Type Checking Arrays • Basic type – array • Number of dimensions • Type of components • Type of subscript • Range of subscript (must be done at runtime, if at all)

  7. 1 dim array Virtual Origin (VO) Lower Bound (LB) Upper Bound (UB) Comp type Comp size (E) A[LB] A[LB+1] A[UB]  Array Layout • Assume one dimension A[0]

  8. Array Component Access • Component access through subscripting, both for lookup (r-value) and for update (l-value) • Component access should take constant time (ie. looking up the 5th element takes same time as looking up 100th element)

  9. Array Access Function • L-value of A[i] = VO + (E * i) =  + (E * (i – LB)) • Computed at compile time • VO =  - (E * LB) • More complicated for multiple dimensions

  10. Array Data VO VO VO VO= Array Access Function • VO can be positive or negative, and can be an address that is before, within, or after the actual storage for the array: • In C and SML, VO = , since LB = 0 always

  11. A[0,0] 2 dim array Component type Virtual Origin (VO) Lower Bound (LB1) Upper Bound (UB1) Lower Bound (LB2) Upper Bound (UB2) Component size (E) A[LB1,LB2] A[LB1,LB2+1] A[LB1,UB2] A[LB1+1,LB2] A[LB1+1,UB2] A[UB1,UB2]  2-Dimensional Array Layout

  12. 2-Dim Array Access Function • S = length of row = (UB2 – LB2 + 1) * E • VO =  - (LB1 * S) – (LB2 * E) • L-value of A[i,j] = VO + (i * S) + (j * E) =  + (i – LB1) * S + (j –LB2) * E

  13. Records • Ordered sequence of fixed number of objects of differing types • Indexed by fixed identifiers called labels or fields • L-value for record element given by more complex accessing formula than for arrays

  14. Record type Num. of components Comp 1 label Comp 1 type Comp 1 location =  Comp n label Comp n type Comp n location Typical Record Layout Descriptor Data R.1 R.2 R.n

  15. Type Checking Record • Basic type – record • Number, name (label) of components • Possibly order of labels • If order matters, labels must be unique • If order doesn’t matter, layout must give a canonical ordering • Type of components per label

  16. Record Layout • Most of descriptor exists only at compile time • Access function: • Comp i location given by • L-value of R.i =  +  (size of R.j) i - 1 j = 1

  17. Lists • Ordered collection of variable number of elements • Many languages (LISP, Scheme, Prolog) allow heterogeneous list • SML has only homogeneous lists

  18. Lists • Layout: linked series of cells (called cons cells) with descriptor, data and pointers • Data in first cell of list called head of list • R-value of pointer in first cell called tail of list

  19. Lists • Sequential access of data by following pointers • Access is linear in position in list • Takes twice as long to look up 10th element as to look up 5th element

  20. Lists • Adding a new element to list done only at head, called consing • Creates new cell with element to be added and pointer to old list (ie. creates new list)

  21. list list list int 1 real 2.5 char ‘a’ List Layout • Example: [1,2.5,’a’]

  22. list list list list int 1 real 2.5 char ‘a’ List Layout • Example: [[1,2.5,],’a’]

  23. Union Types • Set-wise the (discriminated) union of the component types • Interchangeable with variant records as primitive type construct • Elements chosen from one of component types

  24. Union Types • Problem: if int occurs as two different components of union type, can we tell which component an int is for?

  25. Union Types • Two kinds of union types: • Free union - Ans: no • Discriminated union – Ans: yes • If each component is tagged to separate occurrences of same type, discriminated union, otherwise not

  26. Actual data Unused space Union Layout Descriptor Data • No tag if free union • L is fixed length of biggest component Union type Component type Component tag Component location L

  27. Combining Data Structures • Possible to have any of the above structures as components of others • Since lists are of variable size, but arrays must store fixed size element, how to store lists in an array?

  28. Combining Data Structures • Answer: cons cells have uniform size, store just the leading cons cell

  29. list list list list list list int 5 int 6 int 3 int 1 int 7 int 2 Example: • Data in 4-element array of lists

More Related