2
2 Why have Types? Types provide context for operations –a+b  what kind of addition? –pointer p = new object  how much space? Limit valid set of operations –int a = “string”  know error before run time

5
5 Static vs. Dynamic Typing First, what does strongly typed mean? –Prevents invalid operations on a type Statically typed: strongly typed and type checks can be performed at compile time –Ada, Pascal: almost all type checking done at compile time, with just a few exceptions –C, C++, Java, Ocaml Dynamically typed: type checking is delayed until run time –Scheme, Smalltalk, Perl

10
10 Type Equivalence Determining when the types of two values are the same struct student { string name; string address;} struct school { string name; string address;} Are these the same? Should they be?

11
11 Structural Equivalence The same components, put together the same way = same type Algol-68, early Pascal, C (with exceptions) –And ML, somewhat Straightforward and easy to implement Definition varies from lang to lang –eg. Does the ORDER of the fields matter? Back to example: are they the same? struct student { string name; string address;} struct school { string name; string address;} Yes, they are (with structural equivalence)

12
12 Name Equivalence More popular recently A new name or definition = a new type Java, current Pascal, Ada Assume that: if programmer wrote two different definitions, then wanted two types –Is this a good or bad assumption to make? Back to example: are they the same? struct student { string name; string address;} struct school { string name; string address;} No, they are not (with name equivalence)

14
14 Types of Name Equivalence Strict: aliases are distinct types Loose: aliases are equivalent types Ada has both: –type test_score is integer; –type celsius_temp is new integer; type fahren_temp is new integer; A derived type is incompatible with its parent type now f := c will generate an error

15
15 Type Conversion Conversion needed when certain types are expected but not received a+b  expecting either 2 ints or 2 floats Switching between types may result in overflow –example: floating-point  integer

16
16 Coercion Can automatically force values to another type for the given context a+b revisited: –Without coercion, if both aren’t the same type then there’s a compile-time error –With coercion, if either is floating-point, then addition is floating-point; otherwise int addition Issue: Things are happening without you asking them to occur! Good/Bad?

23
23 Variant Records variant records – provide two or more alternative fields. discriminant – the field that determines which alternative fields to use. Useful for when only one type of record can be valid at a given time.

28
28 Arrays Group a homogenous type into indexed memory. Language differences: A(3) vs. A[3]. –Brackets are preferred since parenthesis are typically used for functions/subroutines. Subscripts are usually integers, though most languages support any discrete type.

31
31 Array Memory Allocation An array’s “shape” (dimensions and bounds) determines how its memory is allocated. –The time at which the shape is determined also plays a role in determining allocation. At least 5 different cases for determining memory allocation:

33
33 Array Memory Allocation Local lifetime, bound at elaboration time: –Array’s shape is not known at compile time, and exists only as locally needed. Array is allocated in subroutine’s stack frame and divided into fixed-size and variable-sized parts. main() { var_ptr = new int[size]; } Arbitrary lifetime, bound at elaboration time: –Array is just references to objects. Java does not allocate space; just makes a reference to either new or existing objects. var_ptr = new int[size];

34
34 Array Memory Allocation Arbitrary lifetime, dynamic shape –The array may shrink or grow as a result of program execution. The array must be allocated from the heap. Increasing size usually requires allocating new memory, copying from old memory, then de-allocating the old memory.

35
35 Memory Layout Options Ordering of array elements can be accomplished in two ways: –row-major order – Elements travel across rows, then across columns. –column-major order – Elements travel across columns, then across rows. Row-majorColumn-major

51
51 Dangling References dangling reference – a live pointer that no longer points to a valid object –to heap object: in explicit reclamation, programmer reclaims an object to which a pointer still refers –to stack object: subroutine returns while some pointer in wider scope still refers to local object of subroutine How do we prevent them?

58
58 Reference Counts Idea –Counter in each object that tracks number of pointers that refer to object –Recursively decrement counts for objects and reclaim those objects with count of zero Must identify every pointer –in every object (instance of type) –in every stack frame (instance of method) –use compiler-generated type descriptors

62
62 Conservative collection Idea –Number of blocks in heap is much smaller than number of possible addresses (2 32 ) – a word that could be a pointer into heap is probably pointer into heap –Scan all word-aligned quantities outside the heap; if any looks like block address, mark block useful and recursively explore words in block Advantages –No need for type descriptors –Usually safe, though could “hide” pointers Disadvantages –Some garbage is unclaimed –Can not compact (not sure what is pointer and what isn’t)