Carson X-10NB Chassis User Manual Page 72

  • Download
  • Add to my manuals
  • Print
  • Page
    / 108
  • Table of contents
  • BOOKMARKS
  • Rated. / 5. Based on customer reviews
Page view 71
Pascal
Procedures
By
John
P.
Jones
In
this column I'll give you a few
pointers on pointers. Actually, it'll be
more
than
a few pointers - we're
going
to
wade
right
into this non-
trivial subject.
As
you'll see, pointers
can be a very powerful tool,
but
a lot
of people
don't
use
them
because they
don't
understand them. We'll begin
by
looking
at
allocation of variables, and
then
we'll see
what
that
has
to
do
with pointers.
Static
& Dynamic Variables
Variables
declared
in
the
PRO-
GRAM section and in non-recursive
procedures are
static
variables. This
means their storage is allocated and
fixed
at
compile time. Two mecha-
nisms are built into Pascal for crea-
tion and use of variables
at
run
time
- recursive procedure calls and dy-
namic variables.
Procedures compiled with recursion
enabled will, when called, generate
new copies of their internal variables
on
the
recursion stack. On
return
from
the
procedure, their space is released
for other use. This is all
transparent
to
the
programmer and application.
Pointers
The other mechanism for run-time
variable allocation involves pointer
variables.
A pointer is a variable which con-
tains the address of a variable instead
of
the
data
itself. Pointers are
de-
clared
by
prefixing
the
pointer charac-
ter
('A
')
to
a variable type.
type
strarray
= array[O
••
S]
of
str1ng[16];
1ntptr
=
~1nteger;
aryptr
=
~strarray;
var
dynint
:
1ntptr;
Pointers can be assigned and
com-
pared in much
the
same way as other
variables. The primary function of
pointers, however,
is
to
allow for
the
creation
and
use of dynamic variables.
Free memory is assigned
to
the
heap,
and this area is available for newly
created dynamic variables. The state-
ment:
new
(
dynint)
;
70
will create a variable of
type
INTE-
GER
on the heap, assign the integer's
address
to
dynint, and reduce
the
size
of
the
heap
by
one integer's storage
space.
dyn1nt--
....
->
I
dynint
A
I
Dynamic variables are accessed
by
using
the
pointer character as a suffix
to
the
pointer name. So
the
above
says
that
"dynint"
is the address
while
"dynint
A
"
is
the
value contained
at
the
address.
dynint
A
:=
3;
other1ntvar
:=
dynint
A
;
Two
mutually
exclusive mecha-
nisms are used for
de~allocating
heap
space. Your compiler will have one or
both
of them.
dispose(dynint);
Dispose is
the
complement of New
and will do
what
its
name implies -
dispose of
the
storage for dynint .
The other method for reclaiming
heap space is called
"Mark"
and
it
lets you
mark
a variable so you can
reclaim
it
and everything beyond it.
Hark(dynint)
;
This places a marker on
the
heap
at
dynint
's
position. Later, when
the
statement: RELEASE(dynint) is exe-
cuted, all space allocated from
dynint"-
onward will be de-allocated.
If
your
compiler provides
both
methods, re-
sults will be unpredictable if you use
both
in
the
same program.
If
this
were
as
far as
the
story went,
dynamic variables would
not
be much
more useful
than
statics. Since point-
ers can point
to
records and can also
be fields of records, uses for dynamic
variables are limited only
by
the
imagination.
Linked
Lists
Given
the
following declarations,
type
6245 Columbia Ave.
St. Louis MO 63139
lstptr
= A11st1tem;
11st1tem =
record
itemdata
:
datatype;
nxt~tem
:
lstptr;
end;
var
root,
item
:
lstptr;
we can construct a simple linked
list. The pointer root
is
our anchor on
the world, and
it
points
to
the
start
of
the
list. The list is initialized with:
root
:=
nil;
{nil
is
the
pre-
defined
value
for
a
null
pOinter
}
and
items
are
added
in
this
way:
and items are added in
this
way:
new('item); {
create
new
var
}
itemA.itemdata
:=
data;
{
the
field
"itemdata"
in
record
pOinted
to
by
item
is
assigned
contents
of
data
}
itemA.nxt~tem
:=
root;
{
link
to
prevo
data
}
root
:=
item;
{
reestablish
anchor }
Singly linked lists
as
shown in
Figure 1 can be useful,
but
they
have
disadvantages. First,
the
components
can only be accessed serially from
the
previous item. Another disadvantage
is
that
insertions and deletions can be
cumbersome. Also, if you use a
list
like this, all procedures
that
access
it
must
be sure
to
check for an
empty
list.
The
First
Shall Remain So
By
using an additional reference
pointer
that
always points
to
the
oldest item, a
structure
called a queue
is formed.
See Figure
2.
In
this
example, items are added
using the pointer
LAST, in a way
similar
to
the
above for a simple list.
The additional factor
to
consider is
that
the
initial item should be added
with
the
pointer
FIRST.
Removal of
items from this queue can be done as
shown in Figure
3.
A queue is
thus
a first in, first
out
structure, while
the
earlier simple
linked list is a
last
in, first
out
device.
Micro Cornucopia, Number 25, August-September 1985
Page view 71
1 2 ... 67 68 69 70 71 72 73 74 75 76 77 ... 107 108

Comments to this Manuals

No comments