Top Banner
Chapter Four Data Types Pratt
36

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.

Dec 17, 2015

Download

Documents

Ashlee Foster
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

Chapter Four Data Types

Pratt

Page 2: 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.

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

Page 3: 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.

3

Attributes and Bindings

• Type

• Location

• Value

• Name

Page 4: 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.

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

Page 5: 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.

5

Data Types

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

• Syntactic representation

Page 6: 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.

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.

Page 7: 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.

7

Operations

• Signature of an operation:

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

Page 8: 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.

8

Operations as Mathematical Functions

• Undefined for certain inputs.– Underflow , overflow

• Implicit arguments.

• Side effects (implicit results).

• Self-modification (history sensitive)

Page 9: 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.

9

Implementation

• Storage representation.– Attributes:

• not stored in the runtime storage representation

• run time descriptor

• implementation of operations

Page 10: 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.

10

Declarations

• Choice of storage representation

• Storage management

• Polymorphic operations

• Type checking

Page 11: 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.

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

Page 12: 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.

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.

Page 13: 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.

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.

Page 14: 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.

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)

Page 15: 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.

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

Page 16: 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.

16

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

( Code inserted during compilation)

(p. 126)

Page 17: 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.

17

Two Opposed Philosophies

• No coercions (Pascal, Ada)

• Coercion as a rule (C)

Page 18: 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.

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)

Page 19: 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.

19

Initialization

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

• A serious source of programming errors.

• Explicit , implicit.

Page 20: 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.

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

Page 21: 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.

21

Internationalization

• Sorting

• Case

• Scanning direction

• Country-specific data format

• Country-specific time format

Page 22: 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.

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.

Page 23: 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.

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.

Page 24: 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.

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.

Page 25: 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.

25

Type of Each Component

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

• Heterogeneous.– Records, lists.

Page 26: 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.

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

Page 27: 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.

27

Maximum number of components

• For a variable size data structure.

Page 28: 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.

28

Organization of the components

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

• Multidimensional.– Arrays, record, lists.

Page 29: 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.

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.

Page 30: 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.

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

Page 31: 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.

31

Storage Representation

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

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

Page 32: 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.

32

Implementation of Operations

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

formula. (p. 146)

• Linked representation– following a chain of pointers

Page 33: 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.

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

Page 34: 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.

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)

Page 35: 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.

35

Type Checking

• Existence of a selected component.

• Type of a selected component.

Page 36: 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.

36

Data Structures

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