36
Chapter Four Data Types Pratt

Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system

Embed Size (px)

Citation preview

Chapter Four Data Types

Pratt

2

Data Objects

• A run-time grouping of one or more pieces of data in a virtual machine

• a container for data

• it can be– system defined– programmer defined

3

Attributes and Bindings

• Type

• Location

• Value

• Name

4

Data Types

• A data type is a class of data objects together with a set of operations for creating and manipulating them.

• Specification of a data type:– attributes– valid values– valid operations

• example: specification of an array

5

Data Types

• Implementation of a data type– storage representation of data object– algorithms of valid operations

• Syntactic representation

6

Elementary Data Types

• Elementary data object contains a single data value.

• A class of such data objects and the valid operations: elementary data type.

7

Operations

• Signature of an operation:

op name: arg type * arg type * … *arg type --> result type

8

Operations as Mathematical Functions

• Undefined for certain inputs.– Underflow , overflow

• Implicit arguments.

• Side effects (implicit results).

• Self-modification (history sensitive)

9

Implementation

• Storage representation.– Attributes:

• not stored in the runtime storage representation

• run time descriptor

• implementation of operations

10

Declarations

• Choice of storage representation

• Storage management

• Polymorphic operations

• Type checking

11

Type Checking

• Checking that each operation executed by a program receives the proper number of arguments of the proper data type.

• Dynamic type checking: run-time (type tags for data objects)

• Static type checking: compile-time

12

Dynamic Type Checking

• Advantage: Flexibility

• Disadvantages: – difficult debugging, some paths never checked.– Extra storage for type information during

program execution.– Software simulated type checking, reducing

speed.

13

Static Type Checking

• Information required:– For each operation, the number, order, and data

types of its arguments and results.– For each variable, the type of data object

named.• Always A has the same type (a formal parameter).

– The type of each constant data object.

14

• Strong Typing.– Detect all type errors statically.– A function f f , with signature f : S --> R f : S --> R , is type

safe if execution of f f cannot generate a value outside of R .R .

– Type inference.• ML (p.124)

15

Type Conversion and Coercion

• A type mismatch can cause :– error– coercion (implicit type conversion)

• type conversion:– conversion-op : type1 --> type2

• coercions if no loss of information.– Widening or promotion– Narrowing

16

• What about Coercion– for dynamic type checking? – for static type checking?

( Code inserted during compilation)

(p. 126)

17

Two Opposed Philosophies

• No coercions (Pascal, Ada)

• Coercion as a rule (C)

18

Assignment

• Assignment is the basic operation for changing the binding of a value to a data object.

• In Pascal:– assignment: integer * integer --> void

• In C:– assignment:integer * integer-->integer (p 127)

19

Initialization

• An uninitialized variable: an l-value with no corresponding r-value.

• A serious source of programming errors.

• Explicit , implicit.

20

Elementary Data Types

• Numeric Data Types– Integers– Subranges– Floating-point Real Numbers– Fixed-point Real Numbers

• Enumerations (one of a small number of symbolic values)

• Booleans• Characters

21

Internationalization

• Sorting

• Case

• Scanning direction

• Country-specific data format

• Country-specific time format

22

Structured Data Objects and Data Types

• Structured data object or data structure: a data object that is constructed as an aggregate of other data objects, called components.

• Particular aspects of structured data types: – how to indicate the component data objects of

a data structure and their relationships.– storage management.

23

Specification of data structure types

• Number of components.

• Type of each component.

• Names to be used for selecting components.

• Maximum number of components.

• Organization of the components.

24

Number of Components

• Fixed size.– Arrays, records , character strings.

• Variable size.– Stacks, lists, sets, tables, files, character strings.– Use a pointer data type.– Insert and delete operations.

25

Type of Each Component

• Homogeneous.– Arrays, character strings, sets, files.

• Heterogeneous.– Records, lists.

26

Names to be used for selecting components

• Array: an integer subscript or a sequence of subscripts.

• Record: a programmer defined identifier.

• Stacks and files: ?

27

Maximum number of components

• For a variable size data structure.

28

Organization of the components

• Simple linear sequence .– Vectors, records, strings, stacks, lists, files.

• Multidimensional.– Arrays, record, lists.

29

Operations on Data Structures

• Component selection operations.– Random selection– Sequential selection.

How you select a component?

• Whole-data-structure operations.– Addition(arrays), assignment(records), union(sets).

• Insertion/deletion of components.

• Creation/deletion of data structures.

30

Implementation of Data Structure Types

Storage Representation :

• affected by– efficient selection of components.– efficient overall storage management.

• Includes– storage for the components,– an optional descriptor (for the attributes).

31

Storage Representation

• Sequential representation.– Descriptor and components.– Fixed size.

• Linked representation.– By pointers.– Variable size.

32

Implementation of Operations

• Sequential representation– base-address-plus-offset using an accessing

formula. (p. 146)

• Linked representation– following a chain of pointers

33

Storage Management

• Access path : its name, a pointer.

• Life time of a data object: binding to a storage location.

Two problems:

• garbage

• dangling references

34

• garbage: all access paths to a data object are destroyed but the data object continues to exist (the binding of data object to storage location has not been broken),

• dangling references: an access path that continues to exist after the lifetime of the associated data object.

(p. 149)

35

Type Checking

• Existence of a selected component.

• Type of a selected component.

36

Data Structures

• Vectors and Arrays• Records• Variant Records• Lists• Character Strings• Pointers• Sets• Files