Download presentation
Presentation is loading. Please wait.
Published byAshlee Foster Modified over 9 years ago
2
Chapter Four Data Types Pratt
3
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
4
3 Attributes and Bindings Type Location Value Name
5
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
6
5 Data Types Implementation of a data type –storage representation of data object –algorithms of valid operations Syntactic representation
7
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.
8
7 Operations Signature of an operation: op name: arg type * arg type * … *arg type --> result type
9
8 Operations as Mathematical Functions Undefined for certain inputs. –Underflow, overflow Implicit arguments. Side effects (implicit results). Self-modification (history sensitive)
10
9 Implementation Storage representation. –Attributes: not stored in the runtime storage representation run time descriptor implementation of operations
11
10 Declarations Choice of storage representation Storage management Polymorphic operations Type checking
12
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
13
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.
14
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.
15
14 Strong Typing. –Detect all type errors statically. f f : S --> R f R. –A function f, with signature f : S --> R, is type safe if execution of f cannot generate a value outside of R. –Type inference. ML (p.124)
16
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
17
16 What about Coercion – for dynamic type checking? – for static type checking? ( Code inserted during compilation) (p. 126)
18
17 Two Opposed Philosophies No coercions (Pascal, Ada) Coercion as a rule (C)
19
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)
20
19 Initialization An uninitialized variable: an l-value with no corresponding r-value. A serious source of programming errors. Explicit, implicit.
21
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
22
21 Internationalization Sorting Case Scanning direction Country-specific data format Country-specific time format
23
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.
24
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.
25
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.
26
25 Type of Each Component Homogeneous. –Arrays, character strings, sets, files. Heterogeneous. –Records, lists.
27
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: ?
28
27 Maximum number of components For a variable size data structure.
29
28 Organization of the components Simple linear sequence. –Vectors, records, strings, stacks, lists, files. Multidimensional. –Arrays, record, lists.
30
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.
31
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).
32
31 Storage Representation Sequential representation. –Descriptor and components. –Fixed size. Linked representation. –By pointers. –Variable size.
33
32 Implementation of Operations Sequential representation –base-address-plus-offset using an accessing formula. (p. 146) Linked representation –following a chain of pointers
34
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
35
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)
36
35 Type Checking Existence of a selected component. Type of a selected component.
37
36 Data Structures Vectors and Arrays Records Variant Records Lists Character Strings Pointers Sets Files
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.