360 likes | 474 Views
Chapter Four Data Types. Pratt. 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. Attributes and Bindings. Type Location Value Name. Data Types.
E N D
Chapter Four Data Types Pratt
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
Attributes and Bindings • Type • Location • Value • Name
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
Data Types • Implementation of a data type • storage representation of data object • algorithms of valid operations • Syntactic representation
Elementary Data Types • Elementary data object contains a single data value. • A class of such data objects and the valid operations: elementary data type.
Operations • Signature of an operation: op name: arg type * arg type * … *arg type --> result type
Operations as Mathematical Functions • Undefined for certain inputs. • Underflow , overflow • Implicit arguments. • Side effects (implicit results). • Self-modification (history sensitive)
Implementation • Storage representation. • Attributes: • not stored in the runtime storage representation • run time descriptor • implementation of operations
Declarations • Choice of storage representation • Storage management • Polymorphic operations • Type checking
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
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.
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.
Strong Typing. • Detect all type errors statically. • 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)
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
What about Coercion • for dynamic type checking? • for static type checking? ( Code inserted during compilation) (p. 126)
Two Opposed Philosophies • No coercions (Pascal, Ada) • Coercion as a rule (C)
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)
Initialization • An uninitialized variable: an l-value with no corresponding r-value. • A serious source of programming errors. • Explicit , implicit.
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
Internationalization • Sorting • Case • Scanning direction • Country-specific data format • Country-specific time format
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.
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.
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.
Type of Each Component • Homogeneous. • Arrays, character strings, sets, files. • Heterogeneous. • Records, lists.
Names to be used for selecting components • Array: an integer subscript or a sequence of subscripts. • Record: a programmer defined identifier. • Stacks and files: ?
Maximum number of components • For a variable size data structure.
Organization of the components • Simple linear sequence . • Vectors, records, strings, stacks, lists, files. • Multidimensional. • Arrays, record, lists.
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.
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).
Storage Representation • Sequential representation. • Descriptor and components. • Fixed size. • Linked representation. • By pointers. • Variable size.
Implementation of Operations • Sequential representation • base-address-plus-offset using an accessing formula. (p. 146) • Linked representation • following a chain of pointers
Storage Management • Access path : its name, a pointer. • Life time of a data object: binding to a storage location. Two problems: • garbage • dangling references
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)
Type Checking • Existence of a selected component. • Type of a selected component.
Data Structures • Vectors and Arrays • Records • Variant Records • Lists • Character Strings • Pointers • Sets • Files