HP 3000 Manuals

Procedures for Allocation and Deallocation [ HP Pascal/iX Reference Manual ] MPE/iX 5.0 Documentation


HP Pascal/iX Reference Manual

Procedures for Allocation and Deallocation 

HP Pascal distinguishes two classes of variables.  These are static and
dynamic.

A static variable is explicitly declared in the declaration part of a
block, and may then be referred to by name in the body.  The compiler
allocates storage for this variable when the block is activated.  The
system does not deallocate this space until the process closes the scope
of the variable.

A dynamic variable is not declared and cannot be referred to by name.
Instead, a declared pointer references this variable.  The system
allocates and deallocates storage for a dynamic variable during program
execution as a result of calls to the standard procedures new and
dispose.  HP Pascal also supports the standard procedures mark and
release.  The area of memory reserved for dynamic variables is called the
heap.

Dynamic variables permit the creation of temporary buffer areas in
memory.  Furthermore, since a pointer may be a component of a structured
dynamic variable, it is possible to write programs with dynamic data
structures such as linked lists or trees.

new 

Usage 

     new(p)
     new(p, t1,...,tn)

Parameters 

p          Any pointer variable.

t          A case constant value representing tag values for the pointer
           variable p.

Description 

The procedure new(p) allocates storage for a dynamic variable on the heap
and assigns its identifying value to the pointer variable p.

If the dynamic variable is a record with variants, then the tag may be
used to specify a case constant.  This constant determines the amount of
storage allocated.  For nested variants, the values must be listed
contiguously and in order of their declaration.  The procedure call does
not assign the specified tag values to the tag fields of the dynamic
variable.

If new is called for a record with variants and no case constants are
specified, the compiler determines storage by the size of the fixed part
plus the size of the largest variant.


NOTE You cannot use an entire dynamic record variable allocated with one or more case constants as an actual parameter, or in an assignment statement.
Note that the pointer variable may be a component of a packed structure. Pointer dereferencing accesses the actual values stored in a dynamic variable on the heap. Example PROGRAM show_new (output); TYPE marital_status = (single, engaged, married, widowed, divorced); year = 1900..2100; ptr = ^person_info; person_info = RECORD name: string[25]; birdate: year; next_person: ptr; CASE status: marital_status OF married..divorced: (when: year; CASE has_kids: Boolean OF true: (how_many: 1..50); false: (); ); engaged: (date: year); single : (); END; VAR p : ptr; BEGIN { Various legal calls of new. } . . new(p); { Allocates record of the largest size. } . . new(p,engaged); { Allocates record with variant engaged.} . . new(p,married); { Allocates record with variant married.} . . new(p,widowed,false); { Allocates record with variants widowed . and false.} . END. dispose Usage dispose(p) dispose(p, t1,...,tn) Parameters p A pointer expression that cannot be NIL or undefined. t A case constant value whose value matches the case constant value specified in new. Description This procedure indicates that the storage allocated for the given dynamic variable is no longer needed. It is an error if the argument to dispose is NIL or undefined. After dispose, the system has closed any files in the disposed storage and p is undefined. If the case constant values are specified when calling new, it is an error if the identical constants do not appear as the parameters in the call to dispose. It is also an error if the pointer argument p references a dynamic variable to which another reference exists. This would be the case if it is a reference parameter, part of a reference parameter, or another pointer to it exists elsewhere. Using dispose may be equivalent to executing an empty statement. For more details, see the HP Pascal/iX Programmer's Guide or the HP Pascal/HP-UX Programmer's Guide, depending on your implementation, or the compiler options "HEAP_COMPACT" and "HEAP_DISPOSE" . Example PROGRAM show_dispose (output); TYPE marital_status = (single, engaged, married, widowed, divorced); year = 1900..2100; ptr = ^person_info; person_info = RECORD name: string[25]; birdate: year; next_person: ptr; CASE status: marital_status OF married..divorced: (when: year; CASE has_kids: boolean OF true: (how_many:1..50); false: (); ); engaged: (date: year); single : (); END; VAR p : ptr; BEGIN . . new(p); { Allocates largest variant. } . . dispose(p); { Deallocates record with largest variant. } . . new(p,engaged); { Allocates record with variant engaged. } . . dispose(p,engaged); { Deallocates record with variant engaged. } . . new(p,married,false); { Allocates record with variants married and false.} . dispose(p,married,true); { Error, case constants don't match new. } . . END. mark Usage mark(p) Parameter p A pointer variable. Description The procedure mark(p) marks the allocation state of the heap and sets the value of p to specify that state. In other words, mark saves the allocation state of the heap in p, which must not subsequently be altered by assignment. If altered, the corresponding release cannot be performed. mark is used with release. Example PROGRAM show_markrelease; VAR w,x,y: ^integer; BEGIN . mark(w); . release(w); { Returns heap to state marked by w. } . mark(x); . mark(y); . release(x); { Returns heap to state marked by x. The } . { pointer y no longer marks a heap state. } END. { Release(y) is now an error. } release Usage release(p) Parameter p A pointer variable that previously appeared as a parameter in a call to mark, and should not have been previously passed to release or altered by assignment. Description The procedure release(p) returns the heap to its allocation state when mark was called with a parameter that has the value of p. This has the effect of deallocating any heap variables allocated since the program called mark. The system can then reallocate the released space. The system automatically closes any files in the released area. It is an error if p was not passed as a parameter to mark, or if it was previously passed to release explicitly or implicitly. After release, p is undefined. Example PROGRAM show_markrelease; VAR w,x,y: ^integer; BEGIN . mark(w); . release(w); { Returns heap to state marked by w. } . mark(x); . mark(y); . release(x); { Returns heap to state marked by x. The } . { pointer y no longer marks a heap state. } END. { Release(y) is now an error. }


MPE/iX 5.0 Documentation