Title: Enumerated Types
1(No Transcript)
2Enumerated Types
An enumerated type is one in which all of the
possible values are enumerated in the definition.
Each of the values becomes associated with a
symbolic constant.
Enumerated types enhance readability, reliability.
3Enumerated Types
An enumerated type is created in C/C as
follows enum suit clubs, hearts, spades,
diamonds
This creates a user defined type enum suit. The
identifiers are clubs, hearts, spades, Each
identifier represents a constant integer,
starting at zero.
4Enumerated Types
An enumerated type is created in C/C as
follows enum suit club, diamond, heart,
spade (Default values are club0, diamond1,
heart2, spade3)
A variable of enum suit is created as enum suit
card1,card2 card1 club card2 spade
5Enumerated Types
- Design Issues
- Is a literal constant (the name) allowed to
- appear in more than one type definition?
- If so, how can type checking be performed?
6Enumerated Types
Pascal, C/C Literal constants must be unique
within a referencing environment
Ada Literal constants may be overloaded
Java No enumerated types
7Enumerated Types
Ada type LETTERS is (A, B, C, D, E,
F, G, H, I) type VOWELS is (A, E,
I, O, U) for LETTER in
VOWELS(A)..VOWELS(I) loop
8Arrays
9Array Types
An array is a homogeneous aggregate of data
elements in which each individual element is
identified by its relative offset from the first
element.
10Array Types
- Design Issues
- types for subscripts
- binding of subscript ranges
- when is memory actually allocated?
- limits on array dimensionality
- special operations
(initialization, assignment, slicing, output)
11Array Types subscripts
References to array elements are made with a
specification of the array name and an element
selector, also known as a subscript.
Pascal sum sum ai FORTRAN sum sum
a(i)
12Array Types subscripts
References to array elements are made with a
specification of the array name and an element
selector, also known as a subscript.
subscript types - Pascal, Ada integers,
boolean, character and enumeration subscript
bounds - Pascal user defined C fixed
at 0 to n-1 FORTRAN user defined with
default 1 to n
13Array Types memory allocation
- static
- fixed stack-dynamic
- stack-dynamic
- heap-dynamic
14Array Types memory allocation
- static subscript range is statically
bound static storage allocation - fixed stack-dynamic subscript range is
statically bound storage allocation is done at
run time
15Array Types memory allocation
- stack-dynamic subscript range is
dynamic storage allocation is done at run time - heap-dynamic subscript range is
dynamic dynamic storage allocation at run time
16Array Types dimensionality
- FORTRAN 77 - limited to seven dimensions
- C - one dimension (but arrays can be elements
of arrays) - Pascal - unlimited
17Array Types operations
- Operations which operate on an array as a unit
(rather than one element at a time) - APL - basic arithmetic (,-,,/), element
reversal, row reversal, column reversal,
transpose, inversion, inner product - Ada - assignment, catenation, limited slicing
- C - assignment
- FORTRAN 77 - no array operations, slicing
- (but some simplified syntax for input and
output)
18Implementation of Array Types
Implementing arrays requires more compile time
effort than implementing simple types
The code to allow run-time access of array
elements must be generated at compile time
19Implementation of Array Types
Assume we have an array, list, with a lower bound
of 1 and we wish to access its k th
element listk
20Implementation of Array Types
The computation of the address of listk can be
computed as follows address(listk)
address(list1) (k-1)element_size This
simplifies to address(listk)
address(list1) - element_size
kelement_size
21Implementation of Array Types
The generalized formula for the computation of
the address of listk, for an array with an
arbitrary lower bound can be computed as
follows address(listk)
address(listlower_bound) - lower_bound
element_size kelement_size
22Implementation of Array Types
address(listk)
address(listlower_bound) - lower_bound
element_size kelement_size
list100 list101 list107 list108
23Implementation of Array Types
The compile time descriptor for a
single-dimensioned array has the following form
Array Element type Index type Index lower
bound Index upper bound Address
24Implementation of Multidimensional Arrays
Objects of data types with two or more dimensions
must be mapped into the single-dimensioned memory.
- Two choices for storage schemes
- row major order
- Elements in any row are stored
- contiguously in memory
- column major order
- Elements in any column are stored contiguously
in memory
25Implementation of Multidimensional Arrays
3 4 7 6 2 5 1 3 8
A11 3 A12 4 A13 7 A21
6 A22 2 A23 5 A31 1
A32 3 A33 8
26A11 3 A12 4 A13 7 A21
6 A22 2 A23 5 A31 1
A32 3 A33 8
27Implementation of Array Types
The formula for the computation of the address of
listi,j, for an array with a lower bound of 1,
can be computed as follows
address(listi,j)
address(list1,1) - ((n1)
element_size) ((inj)element_size)
where list has n elements per row
28The compile time descriptor for a
multi-dimensioned array has the following form