290 likes | 421 Views
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:.
E N D
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:
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 */
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 */
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
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)
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]
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)
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
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
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
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
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
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
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
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
Lists • Ordered collection of variable number of elements • Many languages (LISP, Scheme, Prolog) allow heterogeneous list • SML has only homogeneous lists
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
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
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)
list list list int 1 real 2.5 char ‘a’ List Layout • Example: [1,2.5,’a’]
list list list list int 1 real 2.5 char ‘a’ List Layout • Example: [[1,2.5,],’a’]
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
Union Types • Problem: if int occurs as two different components of union type, can we tell which component an int is for?
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
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
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?
Combining Data Structures • Answer: cons cells have uniform size, store just the leading cons cell
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