Вы находитесь на странице: 1из 43

Fundamentals of Computer Engineering 2 Summer 2004

Structured Data Types

Bernhard Westfechtel
Fundamentals of Computer Engineering 2 Summer 2004

Contents of this chapter

‰ Data structures: motivation


‰ Classification of data structures
‰ Declarations of data structures
‰ Accessing and manipulating data structures
‰ Kinds of data structures
» Arrays
» Records
» Pointers
‰ Storage management

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

Classification of data structures

‰ 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

‰ An array is a data structure which is characterized as follows


» Fixed size
» Homogeneous
» One or more dimensions
Ö Vector: one-dimensional array
Ö Matrix: two-dimensional array
Ö Multi-dimensional array (more than two dimensions)

‰ 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

variable name[i1, ..., in] Accessing an array component

‰ The keyword array indicates an array declaration


‰ component type defines the types of components stored in an array
‰ The number and range of indices are enclosed in square brackets [...]
» n defines the dimension of the array
» ld and ud define lower bounds and upper bounds of indices for
dimension d (1 ≤ d ≤ n), i.e., ld ≤ id ≤ ud
‰ Special cases of n
» n = 1: vectors (one-dimensional arrays)
» n = 2: matrices (two-dimensional arrays)

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

‰ Components are stored


sequentially in memory
v[0] 0
v[1] 1 ‰ Addressing of component i:
addr(v[i]) = b + e*i
v[2] 2 » b: base address (start) of the
v[3] 3 vector v
v[4] 4 » e: size of an element
(component)
v[5] 5
‰ The following condition must hold:
v[6] 6
» 0 ≤ i < l (l : length of vector)
v[7] 7
‰ Otherwise, the index is out of
v[8] 8 range
v[9] 9

Bernhard Westfechtel 8
Fundamentals of Computer Engineering 2 Summer 2004

Scalar product

function sp(v, w : array [0:9] of real) return real is


var result : real; var i : integer;
begin
result := 0;
for i from 0 to 9 do
result := result + v[i] * w[i]
end;
return result
end

‰ 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

‰ Problem: sort a vector of elements (integers) in ascending order


» i ≤ j ⇒ v[i] ≤ v[j]
‰ Solution (informal): straight insertion
» For each index i (0 < i < l) in ascending order
Ö Insert v[i] into v[0..i-1] such that v[0..i] is sorted after the insertion

‰ 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

procedure sort(in out v : array [0:7] of integer) is


var i, j, temp : integer;
begin
for i from 1 to 7 do
temp := a[i]; /* Save a[i] */
j := i;
while j > 0 and temp < a[j – 1] do
a[j] := a[j – 1]; /* Move old element up */
j := j – 1
end;
if j < i then a[j] := temp end /* Insert saved value */
end

Bernhard Westfechtel 11
Fundamentals of Computer Engineering 2 Summer 2004

Strings

‰ In many programming languages, strings are represented as vectors of


characters, e.g.,
» var name : array [0:29] of char;
‰ Disadvantages of this solution
» Strings have a fixed length
» String operations are awkward to program
‰ Our approach
» Pre-defined data type string, e.g.,
Ö var name : string;
» Strings may have variable length
» Pre-defined operations (see below)

Bernhard Westfechtel 12
Fundamentals of Computer Engineering 2 Summer 2004

String operations

Operations Explanation Examples

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"

length Length of a string length("computer science") = 16

String constant for the empty


"" s := "";
string (length = 0)
"computer " & "science" =
& String concatenation
"computer science"
Accessing the i-th character s = "computer science" fi
s[i]
(0 ≤ i ≤ length(s) – 1) s[0] = "c", s[15] = "e"

Bernhard Westfechtel 13
Fundamentals of Computer Engineering 2 Summer 2004

Matrices

‰ A matrix is a two-dimensional array


‰ A matrix consists of rows and column

column j

row i m[i,j]

‰ Possible storage representations


» Row-major order: row by row
» Column-major order: column by column

Bernhard Westfechtel 14
Fundamentals of Computer Engineering 2 Summer 2004

Storage representations

Row-major order Matrix m


Column-major order
0 1 2 3
m[0,0] 0 m[0,0] 0
4 5 6 7
m[0,1] 1 m[1,0] 4
8 9 10 11
m[0,2] 2 m[2,0] 8
m[0,3] 3 Addressing m[0,1] 1
m[1,0] 4 m[1,1] 5
‰ Parameters
m[1,1] 5 » r: number of rows m[2,1] 9
m[1,2] 6 » c: number of columns m[0,2] 2
» e: size of element
m[1,3] 7 m[1,2] 6
» b: base address
m[2,0] 8 m[2,2] 10
‰ Row-major order
m[2,1] 9 m[0,3] 3
» addr(m[i,j]) = b + e*(c*i + j)
m[2,2] 10 m[1,3] 7
‰ Column-major order
m[2,3] 11 m[2,3] 11
» addr(m[i,j]) = b + e*(r*j + i)

Bernhard Westfechtel 15
Fundamentals of Computer Engineering 2 Summer 2004

Example: matrix multiplication


procedure mp(in m1, m2 : array [0:9, 0:9] of real; out m : array [0:9, 0:9] of real) is
var temp : real;
var i, j, k : real;
begin
for i from 0 to 9 do
for j from 0 to 9 do
temp := 0;
for k from 0 to 9 do
temp := temp + m1[i, k] * m2[k, j]
end;
m[i, j] := temp
end
end
end

‰ For the sake of simplicity, we assume square matrices


#rows = #columns (= 10)
‰ mij = ∑ m1ik * m2kj (0 ≤ k < 10) (scalar product of row i and column j)
‰ Calculation requires a nested for loop

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

Example: matrix multiplication (revisited)

type matrix is array [0:9, 0:9] of real;


...
procedure mp(in m1, m2 : matrix; out m : matrix) is
var temp : real;
var i, j, k : real;
begin
for i from 0 to 9 do
for j from 0 to 9 do
temp := 0;
for k from 0 to 9 do
temp := temp + m1[i, k] * m2[k, j]
end;
m[i, j] := temp
end
end
end

Bernhard Westfechtel 18
Fundamentals of Computer Engineering 2 Summer 2004

Records

‰ A record (or structure) is a data structure which is characterized as follows


» Fixed size (apart from variant records, not to be treated here)
» Heterogeneous
» Named components
‰ Attributes of a record
» Number of components
» Name of each component
» Data type of each component
‰ Operations on a record
» Component selection: accessing a component via its name
» Component assignment: assigning a new value to a selected
component
» Whole-record operations: assignment and equality check

Bernhard Westfechtel 19
Fundamentals of Computer Engineering 2 Summer 2004

Record type declaration: general form

type type name is


record
component name : component type
...
component name : component type
end;

‰ Any type can be used as a component type


» Records can have records as components fi nested records
‰ A record type may be used as the component type in an array type
declaration
‰ Orthogonal combination of record types and array types

Bernhard Westfechtel 20
Fundamentals of Computer Engineering 2 Summer 2004

Record type declaration: examples

type Name is type Student is


record record
firstName : string; name : Name;
lastName : string address : Address;
end; studentNo : integer;
studyField : string;
type Address is semester : integer
record end;
street : string;
no : integer; type Students is
city : string; array [1:max] of Student;
zipCode : integer /* Assuming a suitably defined
end; constant max */

Bernhard Westfechtel 21
Fundamentals of Computer Engineering 2 Summer 2004

Accessing record components

var student : Student;


var students : Students;
var address : Address;
... ‰ A record component is referenced
student.name.firstName := "John"; using the dot notation
student.name.lastName := "Smith"; » variable name . component name
denotes the component component
address.street := "Broadway"; name of the variable variable name
address.no := 1567; (which must be defined as a
address.city := "New York"; record)
address.zipCode := 10036;
‰ In case of nested records, the dot
notation is applied repeatedly fi path
student.address := address;
expressions
...

students[1] := student;

...

Bernhard Westfechtel 22
Fundamentals of Computer Engineering 2 Summer 2004

Example: points and rectangles


type Point is record x, y : integer end;

function addpoint(pt1, pt2 : Point) return Point is


var pt : Point;
begin
pt.x := pt1.x + pt2.x;
pt.y := pt1.y + pt2.y;
return pt
end; /* Adds two points by adding their components. */

type Rect is record pt1, pt2 : Point end; /* pt1: lower left corner, pt2: upper right corner */

funtion ptinrect(pt : Point, r : Rect) return boolean is


begin
return (pt.x >= r.pt1.x and pt.x <= r.pt2.x) and (pt.y >= r.pt1.y and pt.y <= r.pt2.y)
end; /* Returns true if the point lies within the rectangle */

pt2
pt
pt1

Bernhard Westfechtel 23
Fundamentals of Computer Engineering 2 Summer 2004

Pointers

‰ A pointer is a reference to some data object


» Value: address of some data object
» Address: location where the pointer is stored in memory
‰ Use of pointers
» Dynamic data structures (growing and shrinking during program execution)
Ö Examples: stacks, trees, etc.
» Note
Ö A pointer is an elementary data object
Ö But it is used to build composite data objects

‰ 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

Declaration of pointer types

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

Graphical illustration of pointers

Storage cell Arrow for the Storage cell


for the pointer pointer for the referenced
data object

‰ Each pointer is stored in some memory cell


‰ The memory cell contains the address of the referenced data object
‰ The referenced data object is stored in the memory cell located at the given
address
‰ The reference is illustrated graphically by an arrow

Bernhard Westfechtel 26
Fundamentals of Computer Engineering 2 Summer 2004

Example

p 10

q 20

r 10

‰ p, q, and r have been declared as pointers to integers:


» var p, q, r : Pinteger;
‰ Currently,
» p refers to a data object whose current value is 10
» r refers to a different data object whose current value is 10 as well
» q refers to a data object whose current value is 20

Bernhard Westfechtel 27
Fundamentals of Computer Engineering 2 Summer 2004

Equality check for pointers

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

Assignment for pointers

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

‰ Dereferencing: following a pointer to obtain the value of the referenced data


object
‰ General form
» p-> denotes the data object referenced by the pointer
» p = q fi p-> = q->, but the opposite direction does not hold in general
‰ Examples
» p-> = r-> (= 10), but p # r
» p-> = 10 # q-> = 20

Bernhard Westfechtel 30
Fundamentals of Computer Engineering 2 Summer 2004

Assignment to dereferenced pointers


p 10

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

Dynamic storage management: the heap

‰ The heap consists of an unordered set of storage chunks (sequence of bytes)


‰ The heap is a storage area which is managed by two pre-defined procedures
» new(p) allocates a new storage chunk for a data object referenced by p
» free(p) disposes the storage chunk referenced by p
‰ Heap and stack differ as follows: Heap
» Stack
Ö Allocation of memory on subprogram call
Ö Disposal of memory on subprogram return
Ö Managed automatically
» Heap
Ö Not affected by subprogram calls and returns!
Ö Managed manually

‰ Problems of heap management


» Garbage
Ö Storage chunks which cannot be accessed any more
» Dangling references Storage
Ö Pointers referring to disposed storage chunks
chunk

Bernhard Westfechtel 32
Fundamentals of Computer Engineering 2 Summer 2004

Allocation and disposal of storage chunks

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

Dangling references (1) Global variables


i 5
j 6
program example is p
var i, j : integer; q
var p, q : ref integer;
begin
Heap
i := 5; j := 6;
new(p); 5
p-> := i;
q := p; /* See (1). */
free(p); /* p not needed any more. */ (2) Global variables
q-> := j; /* Error: dangling reference, see (2). */
i 5
end
j 6
/* This example illustrates a dangling reference, i.e., a pointer p
to a storage location which is not available any more. */ q

Heap
Dangling
reference
Bernhard Westfechtel 35
Fundamentals of Computer Engineering 2 Summer 2004

Dynamic data structure: example

‰ To demonstrate dynamic data structures, one of the most widespread and


simplest examples is used: stacks
‰ To avoid confusion:
» In the following, we refer to stacks defined by a programmer, i.e., user
stacks
» In contrast, the system stack is managed by the runtime system of the
programming language
» User stacks are stored on the system heap!

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

The value nil

‰ nil is a special value which may be assigned to any pointer


‰ p := nil fi p “points nowhere”
‰ nil is a defined value, i.e., it may be checked whether p = nil
‰ A pointer p “points anywhere”
» Immediately after its declaration and before any assignment to p
» After execution of free(p)
‰ p = nil (p “points nowhere”) fi
» p may be used in equality checks and assignments
» p may not be dereferenced, i.e., p-> is forbidden
‰ p “points anywhere” fi
» p may not be read, i.e., only assignments to p and calls of new(p) are
allowed
» In particular, p may not be dereferenced, i.e., p-> is forbidden

Bernhard Westfechtel 38
Fundamentals of Computer Engineering 2 Summer 2004

Data declarations

/* Data declarations for a stack of integers */


type Element is
record
number : integer; /* Stored number. */
next : Stack /* Pointer to next element. */
end;

type Stack is ref Element; /* A stack is represented by a pointer to the top element. */

var stack : Stack; /* A variable for a stack of integers */

Bernhard Westfechtel 39
Fundamentals of Computer Engineering 2 Summer 2004

Functions: informal illustrations


empty(stack) isempty(stack)
stack nil stack nil ?

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. */

function isempty (s : Stack) return boolean is


begin
return s = nil
end;
/* Check whether the stack is empty. */

procedure push(in out s : Stack; n : integer) is


var top : Stack; /* Reference to the new element, which is the new top of the stack. */
begin
new(top); /* Allocates a new element on the heap. */
top->.number := n; /* Store new number. */
top->.next := s; /* Store pointer to previous top element */
s := top /* The new stack is the reference to the new top element */
end;

Bernhard Westfechtel 41
Fundamentals of Computer Engineering 2 Summer 2004

Operations on stacks (continued)


function top(s : Stack) return integer is
begin
return s->.number
end;
/* Follow pointer to top element and return number component.
Precondition: stack not empty. */

procedure pop(in out s : Stack) is


var oldtop : Stack;
begin
oldtop := s;
s := s->.next;
free(oldtop)
end;
/* Adjust pointer to top element to the element below the top
element, and free old top element.
Precondition: stack not empty. */

Bernhard Westfechtel 42
Fundamentals of Computer Engineering 2 Summer 2004

Literature

‰ Terrence Pratt, Marvin Zelkovitz: Programming Languages: Design and


Implementation, Prentice Hall, 2001, Chapter 6
‰ Robert W. Sebesta: Concepts of Programming Languages, Addison-Wesley,
2002, Chapter 6

Bernhard Westfechtel 43

Вам также может понравиться