Title: TYPES:
1CHAPTER 4
- TYPES
- DATA REPRESENTATION
2Data Representation
A day of the year is a data object in some
application. In a program, a day can be
represented by an integer between 1 and 366.
January 1 1 -
February 5 36 -
May 6 126 127 ? Depend on number of days of February
3Values and Their Types
- Basic Types
- Integers
- Characters
- Reals
- Booleans
- Structure Types
- Array
- Records
- pointers
4Type Expressions
- How a data representation is built up
- Example array 0..99 of char
- To represent data objects
- To lay out values in the underlying machine
- To check that operator are applied properly
within expressions
5Preview of Type Name
Data Type Pascal C
boolean boolean bool
char char char
integer integer int
real real double
6Layout
A0
A1
A2
T
T
T
Aarray0..2 of T
R.b
R.c
R.a
R record aTabTbcTc end
Tc
Ta
Tb
p
PT
T
A
01101
A Set of 1..5
7Preview of Arrays
- consists of a sequence of element of the same
type - Supports random access to its elements
ARRAY begin-label..end-label OF data-type
Let A is an array variable Ai is the syntax
for the ith element of array A, and i is called
the index of the ith element. Meaning of Random
Access is the time to access Ai is independent
of the value of i
8Preview of Records
- Consists of a set of components, each with its
own type. - The components of a record have names and are
called fields. - Field names are referred to variously as
selectors, field identifiers, or member names.
9Preview of Records (contd)
Example A Entry of the index in a book can be
represented using a combination of basic types.
An entry consists of an index term and a page
number. An integer term character string A
page number integer
TYPE termrep RECORD
spell array0..99 of char
lengthinteger END
index_entry RECORD term
termrep page
integer END
10Enumerations
- A finite sequence of names written between
parentheses.
TYPE day (Mon,Tue,Wed,Thu,Fri,Sat,Sun) TYPE
boolean (true,false)
Name like Mon are treated as constants
Operation apply to enumberation Ordinal Function
ord(x) maps name x to its integer
position. Successor Function succ(x) maps name x
to next name . Predecessor Function pred(x) maps
name x to previous name.
11Integer and Reals
- Largest and smallest numbers determined primarily
by the number of bits in machine word.
Operation in Pascal lt lt ltgt gt gt in -
or / div mod and not
Operation in C ! lt gt lt gt - /
!
12Subranges
- A special case of basic types
- They restrict the range of values of an existing
type.
The syntax of a subrange in Pascal
is ltconstant1gt..ltconstant2gt
13ARRAYSEQUENCES OF ELEMENTs
- An array type has the form
array ltsimplegt of lttypegt
- An array type specifies the index of the first
and last elements of the array and the type of
all elements. - Index of the first element, called the lower
bound - Index of the last element, called the upper
bound
array 1996..2000 of real array
(Mon,Tue,Wed,Thu,Fri) of char array char of
token
14LEXICAL ANALYZER
- Uses an array to map characters to tokens
- Read input characters in expression
- Groups them into tokens
- Arithmetic operators
- Parentheses
- Semicolon
- numbers
- Numbers are made up digits 100 3 digits
15LEXICAL ANALYZER
TYPE token (plus,minus,times,divide,number,lpare
n,rparen,semi) VAR optoken array char of
token Optoken plus Optoken-
minus ...
CASE ch OF , -, , /, (, ),
begin
lookaheadoptokench ch
end 0..9 begin
... lookahead number
end END end of case
16Array Layout
VAR Aarraylow..high of T
- The machine address of an element Ai relative
to the address of the first element.
Formula for address of Ai is best expressed as
iw (base loww)
17Layout of 2D Array
VAR M array lowi..highi OF lowj..highj OF
T VAR M array 1..3 OF 1..2 OF integer
Formula for the Address of Aij
iw jw (base lowiwi lowjwj)
M1
M2
M3
M1,1
M2,2
M1,2
M2,1
M3,1
M3,2
18Storage Allocation
- Storage Allocation for the values and variables
in a procedure or function is done when the
function is called.
Function getnum integer Var value
integer Begin value 0 repeat
value value10 ord(ch) ord(0)
getch until(chlt0) or (chgt9) getnum
value End
19Layout from Allocation
- Array layout in C is done statically at compile
time. - Storage allocation is usually done upon procedure
entry. - Static variable if both layout and allocation are
done statically.
20Static Dynamic Array Bounds
- Static evaluation Array bounds are computed at
compiled at compile time. Pascal allow bounds
like xmin..xmax - Evaluation upon procedure entry In Algol 60,
array bounds were computed when control entered a
procedure. - Dynamic evaluation In C, a expression of the
form new charsize can be evaluated at any time.
21Array values Initialization
EXAMPLE Array Initialization in C. An array
initializer is a sequence of values for array
elements.
int coin 1, 5, 10, 25, 50, 100
22RECORD
- Record type specifies fields
- Variable declaration Allocates Storage
- Operations on Records
- Comparison of Arrays and Records
23Record Field
- Record type with k fields can have the following
ltRecord-typegt Record ltname1gt lttype1gt end
IEEE single format 32 bit
1
23
8
s
exponent
fraction
Example Type scform record
fractionreal exponentinteger
end
24Allocates Storage
Type complex record rereal
imreal end Var
x,y,zcomplex
struct complex double re double
im struct complex x,y,z
- Storage is allocated
- when the template is applied in a variable
declaration - Not when the template is described.
25Operations on Records
- We have to access data in Record by denote record
with its field-name.
ltrecord-variablegt.ltrecord-field-namegt
Example z.Re x.re y.re z.Im x.im
y.im L z.rez.re z.imz.im
26Comparison of Array Record
- Ai can change at run time, depending on the
value of i, but the record field fixed
ltvargt.ltfieldgt at compile time - A Records Fileds have different types, but all
array elements have the same
27UNIONS VARIANT RECORD
- A union is special case of a variant record, with
an empty common part - variant records have a part common to all record
of that type, and a variant part, specific to
some subset of the record.
28Type kind (leaf, unary, binary) mode
record c1 T1 c2
T2 case kkind of
leaf () unary (childT3)
binary (lchild,rchildT4)
end
29SETS
- Set can be implemented efficiently using bits in
the underlying machine. - Operations of sets turn into bit operations
Example Var A set of 1..3 Can denote one of
following sets ,1,2,3,1,2,1,3,2,3,
1,2,3
The set 1,3 can be encoded by the bit vector
101
30Operations on Sets
- Bit vectors allow the following operations on set
- () Union
- (-) Difference
- () Intersection
- (/) Symmetric difference (A-B)U(B-A)
31Pointers
- Can provides indirect to elements of a known
type. - Used as
- Efficiency to move or copy
- Dynamic data grow and shrink during execution
- Fixed size, a single machine
- independent of what they point to
32Pointer Types
lttype-namegt
Example Var X,Y Integer // X Y are Integer
variables P Integer // P points to an
Integer begin X 17 // assign a
value to X P _at_X // assign the
address of X to P Y P //
dereference P assign result to Y end
33Operations on Pointers
- Dynamic allocation on the heap
- Dereferencing
- Assignment
- Equality testing
- Deallocation
34Operations on Pointers(contd)
- Storage for the new structure is taken from a
special area of memory called a heap - Storage exists until it is explicitly deallocated
by executing
new(p) where p is of type pointer to T,
P T p pointer to T P the object
pointed to by p
Dispose(p)
35Serve as links
Static Layout principle. The size and layout of
the storage for each type are known statically,
before a program runs
- Records and pointers use for data structure (grow
and shrink)
TYPE link cell cell record
info integer next link
end
36Memory Leaks
- Storage that is allocated but is inaccessible is
called garbage. - Programs that create garbage are said to have
memory leaks
37Dangling Pointers
- Dangling Pointers is a pointer to storage that is
being used for another purpose the storage has
been deallocated.
Dispose(p)
q
q
38Dangling Pointers Memory Leaks
- Dangling Pointers are unsafe because the result
of dereferencing a dangling pointer is
unpredictable. - Memory leaks can degrad the performance of a
running program
39Pascal
- Pascal prevents dangling pointers except through
dispose by using the following approach
Confine pointers to the heap. Pointers cannot be
used to access storage for variables
The operations on pointers in Pascal stress
safety over flexibility. C stresses flexibility,
trusting the programmer to use it safety.
40Assignment Pointer
41TYPE ERROR CHECKING
- Type of an expression can be inferred at compile
time.
Type system for a language is a set of rules for
associating a type with expressions in the
language.
- Example type system for Fortran
- Variable name I..N have type int, othername
have type real - A number has type real if it contains a decimal
point - operands in expression have the same type.
42Basic rule of type checking
- Arithmetic Operators
- Overloading
If E and F have type int, then EF also has
type int
If E has type int and F has type int, then
EF also has type int If E has type real and F
has type real, then EF also has type real
43Basic rule of type checking
- Type Conversion
- Polymorphism
Int real 23.142 --gt 2.03.142 real
Polymorphic types allow such a data structure to
be defined once and then applied later to any
desired type such as stacks of integers, stacks
of grammar symbols, and so on.
44CLASS TEMPLATEs
Template ltclass List_entrygt Class List Public
List() int size() const Private
int count List_entry entrymax_list
45Type Names Type Equivalence
- What does it mean for two types to be equal?
Example 4.9 Look at the following two types. var
x,y array 0..9 of integer var z array
0..9 of integer
In Pascal, x and y have the same type, because
they are declared together, but z does not. In
the corresponding C fragments, x,y, and z all
have the same type
46Structural Equivalence
- Two type expressions are structurally equivalent
if an only if they are equivalent under the
following three rules
SE1. A type name is structurally equivalent to
itself SE2. Two types are structurally equivalent
if they are formed by applying the same type
constructore to structurally equivalent
types. SE3. After a type declaration, type n T,
the type name n is structurally equivalent to T
47Forms of Name Equivalence
- Pure name equivalence. A type name is equivalent
to itself, but no constructed type is equal to
any other constructed types. - Transitive name equivalence. A type name is
equivalent to itself and can be declared
equivalent to other type names - Type S integer T S U integer
- Type expression equivalence. A type name is
equivalent only to itself.
48Type Equivalence in Pascal/Modula-2
- Type equivalence was left amgiguous in Pascal.
Its successor, Modula-2, avoided ambiguity by
defining two types to be compatible if
C1. They are the same name, or C2. They are s and
t, where s t is a type declaration, or C3. One
is a subrange of the other, or C4. Both are
subranges of the same basic type.
49Type Equivalence in C/C
- C uses structural equivalence for all types
except records or structures in C - This constraint saves C from having to deal with
circular types.
50Circular Types
- can arise when structures and pointers are used
to implement linked data structures - Linked data structures give rise to recursive or
circular types
51Circular Types
- Type linkcell
- Type cell record
- info integer
- next link
- end
Expression Type P link P
cell P.next link P.next cell