Академический Документы
Профессиональный Документы
Культура Документы
Bernhard Westfechtel
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 2
Fundamentals of Computer Engineering 2 Summer 2004
Motivation
So far, data types have been elementary (integers, reals, booleans, etc.)
However, in many applications data are structured, i.e., elementary data
are composed into larger units, e.g.,
» Student data: first name, last name, student no, address
» Address: street, city, ZIP code, country
» Mathematics: vectors, matrices
» Programs: call stack
» Operating systems: job queues
High-level programming languages need to define constructs for
» Declaration of data structures
» Accessing and manipulating (components of) data structures
Bernhard Westfechtel 3
Fundamentals of Computer Engineering 2 Summer 2004
Number of components
» Fixed size
» Variable size
Types of components
» Homogeneous data structure: all components have the same type
» Heterogeneous data structure: components are of different types
Organization of components
» One-dimensional (e.g., vector)
» Multi-dimensional (e.g., matrix)
Bernhard Westfechtel 4
Fundamentals of Computer Engineering 2 Summer 2004
Arrays
Attributes of an array
» Number of dimensions
» Range of each dimension
» Data type of each component
Operations on an array
» Component selection: accessing a component via its index
» Component assignment: assigning a new value to a selected
component
Bernhard Westfechtel 5
Fundamentals of Computer Engineering 2 Summer 2004
General form
var variable name : array [l1 : u1, ..., ln : un] of component type Array declaration
Bernhard Westfechtel 6
Fundamentals of Computer Engineering 2 Summer 2004
Whole-array operations
Whole-array operations
» Operations which operate on arrays as a whole rather than on individual
components
Equality check
» a1 = a2 ¤
Ö a1 and a2 are declared identically
– Component types are equal
– Equal number of dimensions
– Equal ranges for each dimension
Ö All components are equal, i.e., a1[i1, ..., in] = a2[i1, ..., in] for all
[i1, ..., in]
Assignment
» a1 := a2 copies array a2 component-wise into a1
» Same restrictions for the declarations as above
Bernhard Westfechtel 7
Fundamentals of Computer Engineering 2 Summer 2004
Storage representation
Bernhard Westfechtel 8
Fundamentals of Computer Engineering 2 Summer 2004
Scalar product
sp = ∑ vi * wi (0 ≤ i < 10)
Vectors are usually processed with the help of for loops
Bernhard Westfechtel 9
Fundamentals of Computer Engineering 2 Summer 2004
Sorting
Example:
initial 44 55 12 42 94 18 06 67
i=1 44 55 12 42 94 18 06 67
i=2 12 44 55 42 94 18 06 67
i=3 12 42 44 55 94 18 06 67
i=4 12 42 44 55 94 18 06 67
i=5 12 18 42 44 55 94 06 67
i=6 06 12 18 42 44 55 94 67
i=7 06 12 18 42 44 55 67 94
Bernhard Westfechtel 10
Fundamentals of Computer Engineering 2 Summer 2004
Sorting procedure
Bernhard Westfechtel 11
Fundamentals of Computer Engineering 2 Summer 2004
Strings
Bernhard Westfechtel 12
Fundamentals of Computer Engineering 2 Summer 2004
String operations
s := t; /* Copy t into s */
:= Assignment s := "computer";
/*Assign a string constant */
s = "computer science"
Relational operators referring to
=, < , <=, <=, > s > "algorithm"
the lexicographical order
s < "computer program"
Bernhard Westfechtel 13
Fundamentals of Computer Engineering 2 Summer 2004
Matrices
column j
row i m[i,j]
Bernhard Westfechtel 14
Fundamentals of Computer Engineering 2 Summer 2004
Storage representations
Bernhard Westfechtel 15
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 16
Fundamentals of Computer Engineering 2 Summer 2004
Type declarations
Each programming language comes with a set of pre-defined types (e.g.,
integer, boolean, char, real)
In addition, it may support user-defined types
Definition of user-defined types
» Implicit
Ö m : array [0:9, 0:9] of real
implicitly defines square matrices of real elements
» Explicit with the help of a type declaration
Ö type matrix is array [0:9, 0:9] of real
explicitly defines a matrix type which has a name
Ö The name may be used as an abbreviation of its definition, e.g.,
var m : matrix
General form of a type declaration
» type type name is definition
Benefits
» Increased readability
» Consistent use
Bernhard Westfechtel 17
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 18
Fundamentals of Computer Engineering 2 Summer 2004
Records
Bernhard Westfechtel 19
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 20
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 21
Fundamentals of Computer Engineering 2 Summer 2004
students[1] := student;
...
Bernhard Westfechtel 22
Fundamentals of Computer Engineering 2 Summer 2004
type Rect is record pt1, pt2 : Point end; /* pt1: lower left corner, pt2: upper right corner */
pt2
pt
pt1
Bernhard Westfechtel 23
Fundamentals of Computer Engineering 2 Summer 2004
Pointers
Operations on pointers
» Dereferencing: following the reference to the data object
» Assignment: assigning a storage address to a pointer
» Equality check: checks whether two pointers refer to the same storage address
» Memory management (for dynamic data structures)
Ö Allocate a new data object and make the pointer refer to it
Ö Free the data object referenced by a pointer when it is no longer needed
Bernhard Westfechtel 24
Fundamentals of Computer Engineering 2 Summer 2004
General form
type type name is ref referenced type; A pointer type is defined with the
keyword ref (reference)
The referenced type is the type of the
Examples data object to which the pointer refers
type Pinteger is ref integer; to
/* Pointers referencing integers */ It is not allowed to reference a data
object of a different type (typed
type PAddress is ref Address; pointers)
/* Pointers to address records */
Any referenced type is permitted
type PStudents is ref Students;
/* Pointers to vectors of students */
Bernhard Westfechtel 25
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 26
Fundamentals of Computer Engineering 2 Summer 2004
Example
p 10
q 20
r 10
Bernhard Westfechtel 27
Fundamentals of Computer Engineering 2 Summer 2004
p 10
q 20
r 10
General form
» p1 = p2 ¤ p1 and p2 refer to the same storage location
» p1 = p2 compares the pointers and not the referenced data objects!
Example
» p = r is false because p and r refer to different storage locations
Bernhard Westfechtel 28
Fundamentals of Computer Engineering 2 Summer 2004
p 10
q 20
r 10
General form
» p1 := p2
fi p1 and p2 refer to the same storage location after the assignment
fi p1 = p2 holds after the assignment
Example
» After p := q, p and q store the same address
Bernhard Westfechtel 29
Fundamentals of Computer Engineering 2 Summer 2004
Dereferencing of pointers
p 10
q 20
r 10
Bernhard Westfechtel 30
Fundamentals of Computer Engineering 2 Summer 2004
q 20
p-> := 30
p 10
q 30
General form
» p-> := expression
fi The data object referenced by p is assigned the value of expression
fi The referenced data object rather than the pointer is manipulated
fi The assignment has a side effect since it affects all pointers referring
to the same storage location
Example
» After p-> := 30, q-> = 30
Bernhard Westfechtel 31
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 32
Fundamentals of Computer Engineering 2 Summer 2004
p
Allocation
q » new(r) allocates
r storage on the heap
» Afterwards, r refers to
the new storage chunk
Dangling Disposal
reference new(r); » free(p) allocates the
free(p) storage chunk
referenced by p
» Afterwards, p is a
dangling reference
p Ö p “points
q anywhere”
r
Bernhard Westfechtel 33
Fundamentals of Computer Engineering 2 Summer 2004
Garbage
(1) Global variables
program example is i 5
var i : integer;
procedure f(j : integer) is Stack
var p : ref integer; j 5
begin p
new(p);
p-> := j;
/* See (1) for storage state immediately Heap
before returning from the call f(i) */ 5
end;
begin
i := 5;
f(i); (2)
/* p is not available any more, thus the data Global variables
referenced before by p is garbage, see (2). */ i 5
...
end
Heap
Garbage 5
Bernhard Westfechtel 34
Fundamentals of Computer Engineering 2 Summer 2004
Heap
Dangling
reference
Bernhard Westfechtel 35
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 36
Fundamentals of Computer Engineering 2 Summer 2004
Data structure
number 3
element
stack next
number 2
element
next
number 1
element
next nil
Variables for the stack
» stack pointer to top element
Stack element: record of
» number stored number
» next pointer to next element
Bernhard Westfechtel 37
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 38
Fundamentals of Computer Engineering 2 Summer 2004
Data declarations
type Stack is ref Element; /* A stack is represented by a pointer to the top element. */
Bernhard Westfechtel 39
Fundamentals of Computer Engineering 2 Summer 2004
push(stack, 2)
number 2
stack stack next
⇒
number 1 number 1
next nil next nil
top(stack)
number (return number component
2
stack of top element)
pop(stack)
number 2
stack next stack
⇒
number 1 number 1
next NULL next nil
Bernhard Westfechtel 40
Fundamentals of Computer Engineering 2 Summer 2004
Operations on stacks
procedure empty(out s : Stack) is
begin
s := nil
end;
/* Create an empty stack by assigning nil to the stack pointer. */
Bernhard Westfechtel 41
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 42
Fundamentals of Computer Engineering 2 Summer 2004
Literature
Bernhard Westfechtel 43