HP Pascal/iX Programmer's Guide (31502-90023)

6- 10
Mark and Release Procedures
The predefined procedure
mark
takes a pointer variable
p
as a parameter,
marks the state of the heap, and sets the value of
p
to specify that
state.
The pointer variable
p
is called a
mark
(once a pointer variable becomes
a mark, you cannot dereference it). You can allocate heap space beyond
the mark, and then deallocate that space with the predefined procedure
release
.
The predefined procedure
release
takes a mark pointer variable as a
parameter and deallocates the heap space that was dynamically allocated
after the mark was set. Variables in that space become inaccessible.
Files in that space are closed. After
release
executes, the mark pointer
variable is undefined. The procedure
new
can reallocate the released
space (even if the program does not contain the compiler option
HEAP_DISPOSE).
Example 1
PROGRAM prog;
TYPE
ftype = FILE OF integer;
ptype1 = ^ftype;
ptype2 = ^integer;
VAR
fptr : ptype1;
iptr1,
iptr2,
m,
iptr3,
iptr4: ptype2;
BEGIN
new(iptr1); {Allocate heap space to iptr1^}
new(iptr2); {Allocate heap space to iptr2^}
iptr1^ := 0;
iptr2^ := 0;
mark(m); {Mark the heap with m}
new(iptr3); {Allocate heap space to iptr3^}
new(iptr4); {Allocate heap space to iptr4^}
new(fptr); {Allocate heap space to fptr^, a file}
iptr3^ := 0;
iptr4^ := 0;
reset(fptr^); {Open fptr^}
release(m); {Close fptr^, deallocating heap after m}
iptr1^ := 1;
iptr2^ := 2;
iptr3^ := 3; {illegal -- iptr3^ was deallocated}
iptr4^ := 4; {illegal -- iptr4^ was deallocated}
write(fptr^,5); {illegal -- iptr5^ was deallocated}
m^ := 0; {illegal -- cannot assign value to mark pointer}
END.
The parameter of
mark
(the mark) can be any pointer variable.
The parameter of
release
must be a mark--a pointer variable whose current
value was assigned by the
mark
procedure. It is an error to call
release
with a pointer whose current value was not assigned by the
mark
procedure.
Example 2
PROGRAM prog;
TYPE
ptr1 = ^integer;