PL 10 CH 6
PL 10 CH 6
Data Types
Chapter 6 Topics
• Introduction
• Primitive Data Types
• Character String Types
• User-Defined Ordinal Types
• Array Types
• Associative Arrays
• Record Types
• Tuple Types
• List Types
• Union Types
• Pointer and Reference Types
• Type Checking
• Strong Typing
• Type Equivalence
• Theory and Data Types
1-2
Introduction
1-3
Primitive Data Types
1-4
Primitive Data Types: Integer
1-5
Primitive Data Types: Floating Point
1-6
Primitive Data Types: Complex
1-7
Primitive Data Types: Decimal
1-8
Primitive Data Types: Boolean
• Simplest of all
• Range of values: two elements, one for
“true” and one for “false”
• Could be implemented as bits, but often as
bytes
– Advantage: readability
1-9
Primitive Data Types: Character
1-11
Character String Types Operations
• Typical operations:
– Assignment and copying
– Comparison (=, >, etc.)
– Catenation
– Substring reference
– Pattern matching
1-12
Character String Type in Certain
Languages
• C and C++
– Not primitive
– Use char arrays and a library of functions that provide
operations
• SNOBOL4 (a string manipulation language)
– Primitive
– Many operations, including elaborate pattern matching
• Fortran and Python
– Primitive type with assignment and several operations
• Java
– Primitive via the String class
• Perl, JavaScript, Ruby, and PHP
- Provide built-in pattern matching, using regular
expressions
1-13
Character String Length Options
1-14
Character String Type Evaluation
• Aid to writability
• As a primitive type with static length, they
are inexpensive to provide--why not have
them?
• Dynamic length is nice, but is it worth the
expense?
1-15
Character String Implementation
1-16
Compile- and Run-Time Descriptors
Compile-time Run-time
descriptor for descriptor for
static strings limited dynamic
strings
1-17
User-Defined Ordinal Types
1-18
Enumeration Types
1-19
Evaluation of Enumerated Type
1-20
Subrange Types
Day1: Days;
Day2: Weekday;
Day2 := Day1;
1-21
Subrange Evaluation
• Aid to readability
– Make it clear to the readers that variables of
subrange can store only certain range of values
• Reliability
– Assigning a value to a subrange variable that is
outside the specified range is detected as an
error
1-22
Implementation of User-Defined
Ordinal Types
1-23
Array Types
1-24
Array Design Issues
• What types are legal for subscripts?
• Are subscripting expressions in element
references range checked?
• When are subscript ranges bound?
• When does allocation take place?
• Are ragged or rectangular multidimensional
arrays allowed, or both?
• What is the maximum number of subscripts?
• Can array objects be initialized?
• Are any kind of slices supported?
1-25
Array Indexing
1-26
Arrays Index (Subscript) Types
1-27
Subscript Binding and Array Categories
1-28
Subscript Binding and Array Categories
(continued)
1-29
Subscript Binding and Array Categories
(continued)
1-30
Subscript Binding and Array Categories
(continued)
• C and C++ arrays that include static modifier
are static
• C and C++ arrays without static modifier are
fixed stack-dynamic
• C and C++ provide fixed heap-dynamic
arrays
• C# includes a second array class ArrayList
that provides fixed heap-dynamic
• Perl, JavaScript, Python, and Ruby support
heap-dynamic arrays
1-31
Array Initialization
1-32
Heterogeneous Arrays
1-33
Array Initialization
• C-based languages
– int list [] = {1, 3, 5, 7}
– char *names [] = {″Mike″, ″Fred″, ″Mary Lou″};
• Ada
– List : array (1..5) of Integer :=
(1 => 17, 3 => 34, others => 0);
• Python
– List comprehensions
list = [x ** 2 for x in range(12) if x % 3 == 0]
puts [0, 9, 36, 81] in list
1-34
Arrays Operations
• APL provides the most powerful array processing
operations for vectors and matrixes as well as
unary operators (for example, to reverse column
elements)
• Ada allows array assignment but also catenation
• Python’s array assignments, but they are only
reference changes. Python also supports array
catenation and element membership operations
• Ruby also provides array catenation
• Fortran provides elemental operations because
they are between pairs of array elements
– For example, + operator between two arrays results in an
array of the sums of the element pairs of the two arrays
1-35
Rectangular and Jagged Arrays
1-37
Slice Examples
• Python
vector = [2, 4, 6, 8, 10, 12, 14, 16]
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
1-38
Implementation of Arrays
• Access function maps subscript expressions
to an address in the array
• Access function for single-dimensioned
arrays:
address(list[k]) = address (list[lower_bound])
+ ((k-lower_bound) * element_size)
1-39
Accessing Multi-dimensioned Arrays
1-40
Locating an Element in a Multi-
dimensioned Array
•General format
Location (a[I,j]) = address of a [row_lb,col_lb] +
(((I - row_lb) * n) + (j - col_lb)) * element_size
1-41
Compile-Time Descriptors
1-42
Associative Arrays
1-44
Record Types
1-45
Definition of Records in COBOL
1-46
Definition of Records in Ada
• Record structures are indicated in an
orthogonal way
type Emp_Rec_Type is record
First: String (1..20);
Mid: String (1..10);
Last: String (1..20);
Hourly_Rate: Float;
end record;
Emp_Rec: Emp_Rec_Type;
1-47
References to Records
• Record field references
1. COBOL
field_name OF record_name_1 OF ... OF record_name_n
2. Others (dot notation)
record_name_1.record_name_2. ... record_name_n.field_name
1-48
Operations on Records
1-49
Evaluation and Comparison to Arrays
1-50
Implementation of Record Type
1-51
Tuple Types
• ML
val myTuple = (3, 5.8, ′apple′);
- Access as follows:
#1(myTuple) is the first element
- A new tuple type can be defined
type intReal = int * real;
• F#
let tup = (3, 5, 7)
let a, b, c = tup This assigns a tuple to
a tuple pattern (a, b, c)
1-53
List Types
• Lists in LISP and Scheme are delimited by
parentheses and use no commas
(A B C D) and (A (B C) D)
1-55
List Types (continued)
• List Operations in ML
– Lists are written in brackets and the elements
are separated by commas
– List elements must be of the same type
– The Scheme CONS function is a binary operator in
ML, ::
3 :: [5, 7, 9] evaluates to [3, 5, 7, 9]
– The Scheme CAR and CDR functions are named hd
and tl, respectively
1-56
List Types (continued)
• F# Lists
– Like those of ML, except elements are separated
by semicolons and hd and tl are methods of the
List class
• Python Lists
– The list data type also serves as Python’s arrays
– Unlike Scheme, Common LISP, ML, and F#,
Python’s lists are mutable
– Elements can be of any type
– Create a list with an assignment
myList = [3, 5.8, "grape"]
1-57
List Types (continued)
1-58
List Types (continued)
1-59
Unions Types
1-60
Discriminated vs. Free Unions
1-61
Ada Union Types
1-62
Ada Union Type Illustrated
1-63
Implementation of Unions
type Node (Tag : Boolean) is
record
case Tag is
when True => Count : Integer;
when False => Sum : Float;
end case;
end record;
1-64
Evaluation of Unions
1-65
Pointer and Reference Types
1-66
Design Issues of Pointers
1-67
Pointer Operations
1-68
Pointer Assignment Illustrated
1-69
Problems with Pointers
1-70
Pointers in Ada
1-71
Pointers in C and C++
1-72
Pointer Arithmetic in C and C++
float stuff[100];
float *p;
p = stuff;
1-73
Reference Types
1-74
Evaluation of Pointers
1-75
Representations of Pointers
1-76
Dangling Pointer Problem
1-77
Heap Management
1-78
Reference Counter
1-79
Mark-Sweep
1-81
Variable-Size Cells
1-82
Type Checking
1-83
Type Checking (continued)
1-84
Strong Typing
Language examples:
– C and C++ are not: parameter type checking
can be avoided; unions are not type checked
– Ada is, almost (UNCHECKED CONVERSION is loophole)
(Java and C# are similar to Ada)
1-85
Strong Typing (continued)
1-86
Name Type Equivalence
1-87
Structure Type Equivalence
1-88
Type Equivalence (continued)
1-91
Summary
1-92