Download Pointer variables

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Control table wikipedia , lookup

Linked list wikipedia , lookup

Transcript
Chapter 12
Pointers and
linked structures
Introduction

The data structures that expand or contract as required during the
program execution is called dynamic data structures. These
structures are especially useful for storing and processing data sets
whose sizes change during the program execution.

Dynamic data structures are constructed using special variables called
pointer variables (or simply pointers).

The main purpose of this chapter is to give an introduction to pointers
and show how they can be used to implement a dynamic data structure
known as a linked list.
2
Pointer variables

A variable is declared to be a pointer variable by including the pointer
attribute in the attribute list of its declaration:
type, attribute-list, pointer :: pointer-variable
This declares that pointer-variable can be used to access a memory
location where a value of the specified type and attributes can be
stored.
Example: If the data values are strings, then a pointer to a memory
location that can be used to store a string can be declared by
character(8), pointer :: StringPtr
Above pointer variable StringPtr may be used only to access memory
locations in which character strings of length 8 can be stored.
3
Pointer variables

Similarly,
type Inventory_Info
integer :: Number
real :: Price
end type Inventory_Info
type (Inventory_Info), pointer :: InvPtr
declares that InvPtr is a pointer variable that may be used to access
locations where structures of type Inventory_Info are stored.
4
The allocate statement

or
The allocate statement has the form
allocate (list)
allocate (list, stat=status-variable)
The allocate statement was used to allocate memory for run-time
arrays.
 It is also used to acquire memory locations to associate with pointer
variables during program execution. For example, the statement
allocate (StringPtr)
associates with StringPtr a memory location where a string such as
“Computer” can be stored. We say that StringPtr “points” to this
memory location, called a target, and we picture this with a diagram
like the following:

StringPtr -----------> Computer
5
The allocate statement
Each execution of an allocate statement acquires a new memory
location and associates it with the specified pointer. Thus, if TempPtr is
also a pointer declared by
character (8), pointer :: TempPtr
the statement
allocate (TempPtr)
acquires a new memory location pointed to by TempPtr:

StringPtr ------------> Computer
TempPtr ------------->
?
6
Pointer association
 Pointer variables may be in one of three states:
a) Undefined
b) Associated
c) Disassociated
 Like all variables, each pointer variable is initially undefined. When a
pointer points to a target, its status changes to associated:
pointer
??
 If this association is broken without associating the pointer variable with
a new target, the pointer is said to be null or disassociated. A null
pointer is commonly pictured using the ground symbol:
pointer
 Fortran provides the intrinsic function associated to test whether a
pointer variable is associated with a target. A reference of the form
associated (pointer)
returns .true. If pointer is associated with a target and .false. otherwise.
7
Pointer association

The nullify statement can be used to change a pointer variable’s status
to null. This statement has the form
nullify (List-of-pointers)

When execution of a program begins, the program has a “pool” of
available memory locations, called the free store. The effect of an
allocate statement is to
– 1) Remove a block of memory from the free store.
– 2) Allocate that block to the executing program.

Since the size of the free store is limited, each allocate statement
causes the pool of available memory to shrink. If more memory is
needed than is available, program execution is halted unless a stat =
clause is included in the allocate statement. In this case, the integer
variable in the stat = clause will be assigned 0 if allocation is successful
and a positive value otherwise.
8
Pointer association

Memory that is no longer needed can be returned to the free store by
using a deallocate statement of the form
deallocate (list)
or
deallocate (list, stat = status-variable)
In the second form, the integer variable will be assigned 0 if deallocation is
successful and a positive value otherwise, for example, if some of the
pointer variables in the list are null.

Memory returned to the free store can be reallocated by subsequent
allocate statements. The allocate and deallocate operations are thus
complementary.
9
Pointer assignment
If pointer1 and pointer2 have the same type, an assignment statement of the
form
pointer1 => pointer2
causes pointer1 to have the same association status as pointer2 , and if pointer2 is
associated, pointer1 will point to the same target as pointer2. The previous target
(if any) pointed to by pointer1 can no longer be accessed unless it is pointed to
by some other pointer. The following diagrams illustrate:
Before assignment:

pointer1
pointer3
pointer2
After assignment pointer1 => pointer2 :
pointer1
pointer3
pointer2
10
Pointer assignment
Example: Suppose that both StringPtr and TempPtr are pointer variables
declared by
character(8), pointer :: StringPtr, TempPtr
and point to memory locations containing the strings Computer and
Software, respectively:
StringPtr
Computer
TempPtr
Software
The assignment statement
TempPtr => StringPtr
causes TempPtr to point to the same memory location as StringPtr:
11
Pointer assignment
StringPtr
Computer
TempPtr
Software
The string Software stored in the first location can no longer be accessed
(unless it is pointed to by some other pointer).
 A pointer assignment statement may also have the form
pointer variable => target-variable
where target -variable has the same type as pointer-variable but has the
target attribute (and not the pointer attribute). The declaration of such
a variable has the form
type, attribute-list, target :: pointer-variable
This assignment statement causes pointer-variable to point to targetvariable, that is, to the memory location allocation to target-variable.
12
Pointer assignment

To illustrate, consider the declarations:
character (8), pointer :: StringPtr
character (8), target :: Name = “John Doe”
The statement
StringPtr => Name
causes StringPtr to point to the memory location allocated to Name ( at
compiler time):
Name
John Doe
StringPtr
13
Pointers in expressions
One important rule governs the use of pointers in expressions:
When an associated pointer appears in an expression, it is automatically
dereferenced; that is, the value stored in its target is used. If an
associated pointer appears in an input list, an input value will be stored
in its target.
To illustrate, consider the declarations
character (8), pointer :: StringPtr, NamePtr_1, NamePtr_2
character (8), target :: Name_1 = “Mary Doe”, Name_2
character (8) :: Product = “Computer”
and suppose that a memory location has been associated with StringPtr by
allocate (StringPtr)
StringPtr
The statements
StringPtr = “Computer”
and
StringPtr = Product
are ordinary assignment statements and store the string “Computer” in the
memory location pointed to by StringPtr:

14
Pointers in expressions
StringPtr
Computer
Dereferencing also occurs for pointer variables that point to target
variables. For example, suppose that NamePtr1 and NamePtr2 point
to the variables Name_1 and Name_2, respectively:
NamePtr_1 => Name_1
NamePtr_2 => Name_2
The output statement
print *, NamePtr_1
will then display the value stored in Name_1:
Mary Doe
The assignment statement
NamePtr_2 = NamePtr_1
assigns the string Mary Doe stored in Name_1 to Name_2 and is therefore
equivalent to the assignment statement
Name_2 = Name_1

15
Pointers in expressions

As the previous example illustrates, there is a fundamental difference
between the pointer assignment operator (=>) and the ordinary
assignment operator (=). If both TempPtr and StringPtr are associated,
StringPtr
Computer
TempPtr
Software
the statement
TempPtr = StringPtr
copies the contents of the memory location pointed to by StringPtr into the
location pointed to by TempPtr:
StringPtr
Computer
TempPtr
Computer
16
Pointers in expressions

This result is quite different from that produced by the pointer
assignment
TempPtr => StringPtr
which causes TempPtr to point to the same memory location pointed to by
StringPtr:
StringPtr
Computer
TempPtr
Software
17
Example for Pointers
type, public :: point
real :: x, y
end type point
type (point), target :: pt1
type (point), target :: pt
pt => pt1
pt%x = 1.0
! Equivalent to pt1%x = 1.0
pt%y = 2.0
! Equivalent to pt1%y = 2.0
.
.
.
18
Pointers and subprograms
 Pointers (and targets) may be used as arguments of subprograms,
satisfying the following conditions:
1) If a formal argument is a pointer variable, the corresponding actual
argument must also be a pointer variable of the same type.
2) A pointer formal argument cannot have an intent attribute.
3) If a formal argument is a pointer (or target) variable, the subprogram must
have an explicit interface.
 When the subprogram is invoked, the association status of each actual
argument that is a pointer is passed to the corresponding formal
argument.
 The value returned by a function may also be a pointer. In this case, the
value must be returned by using a result clause to return the value
assigned within the function to a local pointer variable.
19
Exercises
module create_destroy
.
.
.
contains
subroutine create()
real, dimension (:), pointer :: p
allocate (p(100))
.
!Error checking omitted
!For clarity
call calculate(p)
end subroutine create
subroutine calculate (x)
real, pointer, dimension (:) :: x
! calculate using x
.
deallocate(x)
.
end subroutine
.
end module create_destroy
!Error checking omitted
!For clarity
20
Exercises
Study pages between 407-434 (Ellis’s
Book)
 Do self-test exercises 14.1 on page
431.

21
Implementing linked lists
 Pointer variables are important because they are used to construct
dynamic structures. In this section we will show how linked lists can be
implemented using structures and pointers.
LINKED LIST
 A linked list consists of a collection of elements called nodes, each of
which stores two items of information:
(1) an element of the list
(2) a link, which is a pointer, that indicates the location of the node containing
the successor of this list element.
 The nodes are represented in Fortran as structures having two kinds of
components, data components and link components.
 The data components have types that are appropriate for storing the
necessary information, and the link components are pointers.
22
Implementing linked lists

For example, the type of the nodes in the linked list
Data Next
NumList
1550
Data Next
1723
Data Next
1996
can be defined by
type List_Node
integer :: Data
type (List_Node), pointer :: Next
end type List_Node
Each node in the list is a structure of type List_Node, consisting of two components.
The first component Data is of integer type and is used to store the data. The
second component Next is a pointer and points to the next node in the list. In
addition to the nodes of the list in which to store the data items, a pointer to the
first node is needed. Thus we declare a pointer variable NumList by
type (List_Node), pointer :: NumList
for the linked list of integers.
23
Constructing a linked list

Suppose that the integers 1723 and 1996 have already been stored in
a linked list:
Data Next
Data Next
NumList
1723
1996
and suppose that we wish to add 1550 to this list. In the construction, we
use two pointers, NumList to point to the first node in the list and
TempPtr as a temporary pointer:
type (List_Node), pointer :: NumList, TempPtr
we first acquire a new node temporarily pointed to by TempPtr,
allocate (TempPtr)
and store 1550 in the data component of this structure:
TempPtr%Data = 1550
TempPtr
NumList
1550
1723
1996
24
Constructing a linked list

This node can then be joined to the list by setting its link component so
that it points to the first node:
TempPtr%Next => NumList
TempPtr
1550
NumList
1723
1996
The pointer NumList is then updated to point to this new node:
Numlist => TempPtr
TempPtr
NumList
1550
1723
1996
25
Constructing a linked list

To construct the entire list, we could first initialize an empty list:
nullify (NumList)
and then repeat the preceding four statements three times, replacing 1550
by 1996 in the second assignment statement, then by 1723, and finally
again using the value 1550. In practice, however, such linked lists are
usually constructed by reading the data values rather than by assigning
them with assignment statements. In this example, the linked list could
be constructed by using the following program segment, where Item
and AllocateStatus are integer variables.
26
Constructing a linked list
! Construction of a linked list
! Initially the list is empty
nullify (NumList)
! Read the data values and construct the list
do
read *, Item
if (Item == End_Data_Flag) exit
allocate (TempPtr, stat = AllocateStatus)
if (AllocateStatus /= 0) stop “*** Not enough memory ***”
TempPtr%Data = Item
TempPtr&Next => NumList
NumList => TempPtr
end do
27
Exercises


Study pages between 434-451 (Ellis’s Book).
Do self-test exercises 14.2 on page 449.
28