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

6- 4
New Procedure
The predefined procedure
new
takes a pointer variable as a parameter,
allocates a variable of the type that the pointer references, and
"points" the pointer at the new variable (that is,
new
assigns the
address of the new variable to the pointer). The program can then access
the new variable by dereferencing the pointer.
Example 1
PROGRAM prog;
TYPE
iptr = ^integer;
cptr = ^char;
rptr = ^real;
VAR
ivar : iptr; {pointer to a dynamic integer variable}
cvar : cptr; {pointer to a dynamic character variable}
rvar : rptr; {pointer to a dynamic real variable}
BEGIN
new(ivar); {allocate new integer variable on heap}
new(cvar); {allocate new character variable on heap}
new(rvar); {allocate new real variable on heap}
ivar^ := 375; {assign value to new integer variable}
cvar^ := 'c'; {assign value to new character variable}
rvar^ := 3.7; {assign value to new real variable}
END.
The new variable is allocated space on the heap. A run-time error occurs
if the heap cannot accommodate the variable.
If the new variable is a record with variant fields, you can specify the
variant that you want with a tag. The tag only tells the
new
procedure
how much space to allocate; it does not cause the
new
procedure to assign
the value of the tag to the new variable's tag field.
Example 2
PROGRAM prog;
TYPE
marital_status = (single, married);
rec = RECORD
lname,
fname : string[30];
kids : 1..20;
(
Example is continued on next page
.)
CASE mstat : marital_status OF
single : (divorced,
widowed,
engaged : Boolean);
married : (how_many_times: 1..10;
how_long_this_time : 1..100);
END;
recptr = ^rec;
VAR
person1,
person2,
person3 : recptr;
BEGIN
new(person1,single);
WITH person1^ DO BEGIN
lname := 'Doe';
fname := 'John';
kids := 0;