This presentation is the property of its rightful owner.
1 / 82

# Chapter 7: Data Types PowerPoint PPT Presentation

Chapter 7: Data Types. Chapter 7: Data type. 7.1 Type Systems 7.2 Type Checking 7.3 Records (Structures) and Variants (Unions) 7.4 Arrays 7.5 Sets 7.6 Lists 7.7 Pointers and Recursive types. Data Types. Computers manipulate sequences of bits

Chapter 7: Data Types

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

## Chapter 7: Data Types

### Chapter 7: Data type

7.1 Type Systems

7.2 Type Checking

7.3 Records (Structures) and Variants (Unions)

7.4 Arrays

7.5 Sets

7.6 Lists

7.7 Pointers and Recursive types

### Data Types

• Computers manipulate sequences of bits

• But most programs manipulate more general data

• Numbers

• String

• Lists

• Programming languages provide data types that raise the level of abstraction from bits to data

• But computer hardware only knows about bits!

### Data TypesThe Purpose of Types

• Types provide implicit context

• E.g. the expression a+b in Java may be adding two integer, two floats or two strings depending on the context

• Types provides a set of semantically valid operations

• Compilers can detect semantic mistakes

• Make sure that certain meaningless operations do not occur. Type checking cannot prevent all meaningless operations, but it catches enough of them to be useful.

### Type Systems

• High-level languages have type systems

• All objects and expressions have a type

• E.g.int (*)(const void *, const void *)is the type of a C++ function: int compare(const void *, const void *)

• A type system consists of

• A mechanism for defining types and associating them with certain language constructs

• A set of rules for type checking

• Type equivalence

• Type compatibility

• Type inference

### Type SystemsType Checking

• Type checking is the process of ensuring that a program obeys the language’s type compatibility rules

• Strongly typed languages always detect types errors

• Weakly typed languages do not

• It means that the language prevents you from applying an operation to data on which it is not appropriate.

• All expressions and objects must have a type

• All operations must be applied in appropriate type contexts

### Type SystemsStatic Type Checking

• Static Typing means that the compiler can do all the checking at compile time.

• Common Lisp is strongly typed, but not statically typed.

• Pascal is almost statically typed.

• Java is strongly typed, with a non-trivial mix of things that can be checked statically and things that have to be checked dynamically.

### Type SystemsDynamic Type Checking

• Dynamic type checking is performed at run time. It is a form of late binding.

• It tends to be found in languages that delay other issues as well.

• Lisp, Scheme, and Smalltalk are dynamically typed

• Languages with dynamic scoping are generally dynamically typed.

### What is a type?

• Three points of view:

• Denotational: a set of values

• Constructive: a type is built-in type or a composite type

• Composite types are created using type constructors

• E.g. In Java, boolean is a built-in type, while boolean[] is a composite type

• Abstraction-based: a type is an interface that defines a set of consistent operations

• These points of view complement each other

### Classification of TypesBuilt-in Types

• Built-in/Primitive/Elementary types

• Mimic hardware units

• E.g. boolean, character, integer, real (float)

• Their terminology and implementation varies across languages

• Characters are traditionally one-byte quantities using the ASCII character set

• Early computers had a different byte sizes

• Byte = 8 bits standardized by Fred Brooks et al. thanks to the IBM System/360

• Other character sets have also been used

• Newer languages use the Unicode character set

### Built-in TypesNumeric Types

• Most languages support integers and floats

• The range of value is implementation dependent

• Some languages support other numeric types

• Complex numbers (e.g. Fortran, Python)

• Rational numbers (e.g. Scheme, Common Lisp)

• Signed and unsigned integers (e.g. C, Modula-2)

• Fixed point numbers (e.g. Ada)

• Some languages distinguish numeric types depending on their precision

• Single vs. double precision numbers

• C’s int (4 bytes) and long (8 bytes)

### Classification of TypesEnumerations

• Enumeration improve program readability and error checking

• They were first introduced in Pascal

• E.g. type weekday = (sun, mon, tue, wed, thu, fri, sat);

• They define an order, so they can be used in enumeration-controlled loops

• The same feature is available in C

• enum weekday {sun, mon, tue, wed, thu, fri, sat};

• Other languages use constants to define enumeration

• Pascal’s approach is more complete: integers and enumerations are not compatible

### Classification of TypesSubranges

• Subranges improve program readability and error checking

• They were first introduced in Pascal

• E.g.

type test_score = 0..100;

type workday = mon..fri;

• They define an order, so they can be used in enumeration-controlled loops

• The distinction between derived types and subranges (i.e., subtypes) is a feature of Ada.

type test_score is new integer range 0..100;

subtype workday is weekday range mon..fri;

### Classification of TypesComposite Types

• Composite/Constructed types are created applying a constructor to one or more simpler types

• Examples

• Records

• Variant Records

• Arrays

• Sets

• Pointers

• Lists

• Files

### Classification of TypesOrthogonality

• Orthogonality is an important property in the design of type systems

• A collection of features is orthogonal if there are no restrictions on the ways in which the features can be combined.

• Pascal is more orthogonal than Fortran, because it allows arrays of anything, for instance.

• Orthogonality is nice primarily because it makes a language easy to understand, easy to use, and easy to reason about.

### Chapter 7: Data type

7.1 Type Systems

7.2 Type Checking

7.3 Records (Structures) and Variants (Unions)

7.4 Arrays

7.5 Sets

7.6 Lists

7.7 Pointers and Recursive types

### Type Checking

• Type equivalence

• When are the types of two components the same?

• Type compatibility

• When can a value of type A be used in a context that expects type B?

• Type inference

• What is the type of an expression, given the types of the operands?

### Type Checking

• Type equivalence

• - When are the types of two objects the same?

• Type compatibility

• - When can a value of type A be used in a context that expects type B?

• Type inference

• - What is the type of an expression, given the types of the operands?

### Type Checking

• Type equivalence

• - When are the types of two objects the same?

Why is this important?

The compiler wants to determine if an object can be used in a certain context.

At a minimum, the object can be used if its type and thetype expected are equivalent.

### Type Equivalence

• Type equivalence is defined in two principal ways

• Structural equivalence

• Name equivalence

• Two types are structurally equivalent if they have identical type structures

• They must have the same components

• E.g. Fortran

• Two type are nominally equivalent (name equivalent) if they have the same name

• E.g. Java

• Name equivalence is more fashionable these days

### Type EquivalenceStructural Equivalence

• Structural equivalence depends on simple comparison of type descriptions.

How to compare two structures: substitute all names and expand all the way to built-in types. Original types are equivalent if the expanded type descriptions are the same

• Pointers complicate matters, but the Algol folks figured out how to handle it in the late 1960's. The simple (not quite correct) approach is to pretend all pointers are equivalent.

### Type EquivalenceStructural Equivalence

• Consider these two structures

typedef struct { int a, b; } foo1;

typedef struct {

int a, b;

} foo2;

• All languages consider these two types structurally equivalent

### Type EquivalenceStructural Equivalence

• Is the order in the declaration relevant?

typedef struct {

int b;

int a;

} foo2;

typedef struct {

int a;

int b;

} foo1;

• Most languages consider these two types structurally equivalent

### Type EquivalenceStructural Equivalence Pitfall

• Are these two types structurally equivalent?

typedef struct {

} student;

typedef struct {

} school;

• They are, and it is unlikely that the programmer intended to allow these two types to be usedin the same context (although the compiler willhave no trouble doing so).

### Type EquivalenceName Equivalence

• Name Equivalence assumes type definitions with different names are different types

• Otherwise, why did the programmer create two definitions?

• It solves the previous problem

• student and school are not nominally equivalent

• Aliases:

• Under strict name equivalence, aliases are not equivalent

• type A = B is a definition (i.e. new object)

• Under loose name equivalence, aliases are equivalent

• type A = B is a declaration (i.e. binding)

### Type EquivalenceName Equivalence and Aliases

• Loose name equivalence may introduce undesired type equivalences

### 7.2.2 Type Conversion

• A values of one type can be used in a context that expects another type using type conversion or type cast

• Two flavors:

• Converting type cast: underlying bits are changed

• E.g. In C,

int i; float f = 3.4;

i = int(f);

• Nonconverting type cast: underlying bits are not altered

• E.g. In C,

int i; float f; int j = 4;

i = j;

i = *((int *) &f);

### Notes

Does Java use structural or name equivalence?

Make an entry in your Java notebook for this.

p 419

### Type Checking

• Type equivalence

• - When are the types of two objects the same?

• Type compatibility

• - When can a value of type A be used in a context that expects type B?

• Type inference

• - What is the type of an expression, given the types of the operands?

### 7.2.3 Type Compatibility

• Most languages do not require type equivalence in every context

• Instead, a value's type is required to be compatible with that of the context in which it appears.

• Java example:

result = tirandXmlClient.process(GET_COST, xmlString);

Assume that process() returns an Account object. What is the type of result?

### 7.2.3 Type Compatibility

• In Ada, two types T and S are compatible if any of the following conditions is true:

• T and S are equivalent

• T is a subtype of S

• S is a subtype of T

• T and S are arrays with the same number of elements and the same type of elements

### Type Compatibility

• Implicit type conversion due to type compatibility are known as type coercions

• They may involve low-level conversions

• Example:

type weekday is (sun,mon,tue,wed,thur,fri,sat);

subtype workday is weekday range mon..fri;

### Type Compatibility

• Type coercions make type systems weaker

• Example:

### Type Compatibility

• Generic container objects require a generic reference type

• Example

### Type Checking

• Type equivalence

• - When are the types of two values the same?

• Type compatibility

• - When can a value of type A be used in a context that expects type B?

• Type inference

• - What is the type of an expression, given the types of the operands?

### Type Inference

• Type inference

• What is the type of an expression, given the types of the operands?

• Generally easy, but subranges and composites may complicate this issue

Example

result = 5 + 7.5 + sin(j) - 10.5E-12

### An Example

Strict name equivalence:

types are equivalent if they refer to the same declaration

Loose name equivalence

types are equivalent if they refer to the same outermost constructor,

i.e. if they refer to the same declaration after factoring out any type aliases.

Example:

type alink = pointer to cell;

p, q : pointer to cell;

### An Example

Structural equivalence says all five variables have same type.

Strict name equivalence equates types of p & q and r & u, because they refer back to the same type declaration (a constructor on the RHS of a var declaration is an implicit declaration of an ANONYMOUS type)

Loose name equivalence equates types of p & q and r, s, & u because in both cases we can trace back to the same "^" constructor.

### Chapter 7: Data type

7.1 Type Systems

7.2 Type Checking

7.3 Records (Structures) and Variants (Unions)

7.4 Arrays

7.5 Sets

7.6 Lists

7.7 Pointers and Recursive types

### Records

• Also known as ‘structs’ and ‘types’.

• C

struct resident {

char initials[2];

int ss_number;

bool married;

};

• fields – the components of a record, usually referred to using dot notation.

### Nesting Records

• Most languages allow records to be nested within each other.

• Pascal

type two_chars = array [1..2] of char;

type married_resident = record

initials: two_chars;

ss_number: integer;

incomes: record

husband_income: integer;

wife_income: integer;

end;

end;

### Memory Layout of Records

• Fields are stored adjacently in memory.

• Memory is allocated for records based on the order the fields are created.

• Variables are aligned for easy reference.

Optimized for space

Optimized for memory alignment

4 bytes / 32 bits

4 bytes / 32 bits

### Simplifying Deep Nesting

• Modifying records with deep nesting can become bothersome.

book[3].volume[7].issue[11].name := ‘Title’;

book[3].volume[7].issue[11].cost := 199;

book[3].volume[7].issue[11].in_print := TRUE;

• Fortunately, this problem can be simplified.

• In Pascal, keyword with “opens” a record.

with book[3].volume[7].issue[11] do

begin

name := ‘Title’;

cost := 199;

in_print := TRUE;

end;

### Simplifying Deep Nesting

• Modula-3 and C provide better methods for manipulation of deeply nested records.

• Modula-3 assigns aliases to allow multiple openings

with var1 = book[1].volume[6].issue[12],

var2 = book[5].volume[2].issue[8]

DO

var1.name = var2.name;

var2.cost = var1.cost;

END;

• C allows pointers to types

• What could you write in C to mimic the code above?

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

### Variant Records – Pascal Example

type resident = record

initials: array [1..2] of char;

case married: boolean of

true: (

husband_income: integer;

wife_income: integer;

);

false: (

income: real;

);

id_number: integer;

end;

Case is TRUE

Case is FALSE

### Unions

• A union is like a record

• But the different fields take up the same space within memory

union foo {

int i;

float f;

char c[4];

}

• Union size is 4 bytes!

### Union example (from an assembler)

union DisasmInst {

#ifdef BIG_ENDIAN

struct { unsigned char a, b, c, d; } chars;

#else

struct { unsigned char d, c, b, a; } chars;

#endif

int intv;

unsigned unsv;

struct { unsigned offset:16, rt:5, rs:5, op:6; } itype;

struct { unsigned offset:26, op:6; } jtype;

struct { unsigned function:6, sa:5, rd:5, rt:5, rs:5,

op:6; } rtype;

};

### Another union example

void CheckEndian() {

union {

char charword[4];

unsigned int intword;

} check;

check.charword[0] = 1;

check.charword[1] = 2;

check.charword[2] = 3;

check.charword[3] = 4;

#ifdef BIG_ENDIAN

if (check.intword != 0x01020304) {/* big */

cout << "ERROR: Host machine is not Big Endian.\nExiting.\n";

exit (1);

}

#else

#ifdef LITTLE_ENDIAN

if (check.intword != 0x04030201) {/* little */

cout << "ERROR: Host machine is not Little Endian.\nExiting.\n";

exit (1);

}

#else

cout << "ERROR: Host machine not defined as Big or Little Endian.\n";

cout << "Exiting.\n";

exit (1);

#endif // LITTLE_ENDIAN

#endif // BIG_ENDIAN

}

### Chapter 7: Data type

7.1 Type Systems

7.2 Type Checking

7.3 Records (Structures) and Variants (Unions)

7.4 Arrays

7.5 Sets

7.6 Lists

7.7 Pointers and Recursive types

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

### Array Dimensions

• C uses 0 -> (n-1) as the array bounds.

• float values[10];// ‘values’ goes from 0 -> 9

• Fortran uses 1 -> n as the array bounds.

• real(10) values! ‘values’ goes from 1 -> 10

• Some languages let the programmer define the array bounds.

• var values: array [3..12] of real;

(* ‘values’ goes from 3 -> 12 *)

### Multidimensional Arrays

• Two ways to make multidimensional arrays

• Construct specifically as multidimensional.

matrix: array (1..10, 1..10) of real;

-- Reference example: matrix(7, 2)

• Looks nice, but has limited functionality.

• Construct as being an array of arrays.

matrix: array (1..10) of array (1..10) of real;

-- Reference example: matrix(7)(2)

• Allows us to take ‘slices’ of data.

### 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:

### 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-major

Column-major

day[0]

S

u

n

d

a

y

M

o

n

day[1]

d

a

y

T

u

e

s

d

a

day[2]

y

W

e

d

n

e

s

d

a

day[3]

y

T

h

u

r

s

d

a

y

day[4]

F

r

i

d

a

y

S

a

day[5]

t

u

r

d

a

y

day[6]

### Row Pointers vs. Contiguous Allocation

• Row pointers – an array of pointers to an array. Creates a new dimension out of allocated memory.

• Avoids allocating holes in memory.

Array = 57 bytes

Pointers = 28 bytes

Total Space = 85 bytes

### Row Pointers vs. Contiguous Allocation

• Contiguous allocation - array where each element has a row of allocated space.

• This is a true multi-dimensional array.

• It is also a ragged array

Array = 70 bytes

• Calculate the size of an element (1D)

• Calculate the size of a row (2D)

• row = element_size * (Uelement - Lelement + 1)

• Calculate the size of a plane (3D)

• plane = row_size * (Urows - Lrows + 1)

• Calculate the size of a cube (4D)

:

:

A

A(i)

A(i, j)

A(i, j, k)

Memory

• Address of a 3-dimenional array A(i, j, k) is:

+ ((i - Lplane) * size of plane)

+ ((j - Lrow) * size of row)

+ ((k - Lelement)* size of element)

### Chapter 7: Data type

7.1 Type Systems

7.2 Type Checking

7.3 Records (Structures) and Variants (Unions)

7.4 Arrays

7.5 Sets

7.6 Lists

7.7 Pointers and Recursive types

### Sets

• Introduced by Pascal, found in most recent languages as well.

• Common implementation uses a bit vector to denote “is a member of”.

• Example:

U = {‘a’, ‘b’, …, ‘g’}

A = {‘a’, ‘c’, ‘e’, ‘g’} = 1010101

• Hash tables needed for larger implementations.

• Set of integers = (232 values) / 8 = 536,870,912 bytes

### Enumerations

• enumeration – set of named elements

• Values are usually ordered, can compare

enum weekday {sun,mon,tue,wed,thu,fri,sat}

if (myVarToday > mon) { . . . }

• Compiler can catch some errors

• Is sun==0 and mon==1?

• C/C++: yes; Pascal: no

• Can also choose ordering in C

enum weekday {mon=0,tue=1,wed=2…}

### Chapter 7: Data type

7.1 Type Systems

7.2 Type Checking

7.3 Records (Structures) and Variants (Unions)

7.4 Arrays

7.5 Sets

7.6 Lists

7.7 Pointers and Recursive types

### Lists

• list – the empty list or a pair consisting of an object (list or atom) and another list

(a . (b . (c . (d . nil))))

• improper list – list whose final pair contains two elements, as opposed to the empty list

(a . (b . (c . d)))

• basic operations: cons, car, cdr, append

• list comprehensions (e.g. Miranda and Haskell)

[i * i | i <- [1..100]; i mod 2 = 1]

### Chapter 7: Data type

7.1 Type Systems

7.2 Type Checking

7.3 Records (Structures) and Variants (Unions)

7.4 Arrays

7.5 Sets

7.6 Lists

7.7 Pointers and Recursive types

### Pointers

• pointer – a variable whose value is a reference to some object

• pointer use may be restricted or not

• only allow pointers to point to heap (e.g. Pascal)

• allow “address of” operator (e.g. ampersand in C)

• pointers not equivalent to addresses!

• how reclaim heap space?

• explicit (programmer’s duty)

• garbage collection (language implementation’s duty)

### Recursive Types

• recursive type - type whose objects may contain references to other objects of the same type

• Most are records (consisting of reference objects and other “data” objects)

• Used for linked data structures: lists, trees

struct Node {

Node *left, *right;

int data;

}

### Recursive Types

• In reference model of variables (e.g. Lisp, Java), recursive type needs no special support. Every variable is a reference anyway.

• In value model of variables (e.g. Pascal, C), need pointers.

### Value vs. Reference

• Functional languages

• almost always reference model

• Imperative languages

• value model (e.g. C)

• reference model (e.g. Smalltalk)

• implementation approach: use actual values for immutable objects

• combination (e.g. Java)

### Value Model – More on C

• Pointers and single dimensional arrays interchangeable, though space allocation at declaration different

int a[10]; int *b;

• For subroutines, pointer to array is passed, not full array

• Pointer arithmetic

int a[10];

int n;

n = *(a+3);

• <Pointer, Pointer> subtraction and comparison

int a[10];

int * x = a + 4;

int * y = a + 7;

int closer_to_front = x < y;

### 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?

### Dangling References

• Prevent pointer from pointing to objects with shorter lifetimes (e.g. Algol 68, Ada 95). Difficult to enforce

• Tombstones

• Locks and Keys

### Tombstones

• Idea

• Introduce another level of indirection: pointer contain the address of the tombstone; tombstone contains address of object

• When object is reclaimed, mark tombstone (zeroed)

• Create tombstone

• Check validity of access

• Double indirection

• when to reclaim??

• Extra benefits

• easy to compact heap

• works for heap and stack

### Locks and Keys

• Idea

• Every pointer is <address, key> tuple

• Every object starts with same lock as pointer’s key

• When object is reclaimed, object’s lock marked (zeroed)

• No need to keep tombstones around

• Objects need special key field (usually implemented only for heap objects)

• Probabilistic protection

• Lock to key comparison costly

### Garbage Collection

• Language implementation notices when objects are no longer useful and reclaims them automatically

• essential for functional languages

• trend for imperative languages

• When is object no longer useful?

• Reference counts

• Mark and sweep

• “Conservative” collection

### 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

### Type descriptors example

public class MyProgram {

public void myFunc () {

Car c;

}

}

public class Car {

char a, b, c;

Engine e;

Wheel w;

}

public class Engine {

char x, y;

Valve v;

}

public class Wheel {...}

public class Valve {...}

### Mark-and-Sweep

• Idea

… when space low

• Mark every block “useless”

• Beginning with pointers outside the heap, recursively explore all linked data structures and mark each traversed as useful

• Return still marked blocks to freelist

• Must identify pointers

• in every block

• use type descriptors

Reference Count

Will never reclaim circular data structures

Must record counts

Mark-and-Sweep

Bursts of activity (when collection performed, usually when space is low)

### Conservative collection

• Idea

• Number of blocks in heap is much smaller than number of possible addresses (232) – 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