Sie sind auf Seite 1von 1

such a node is allocated, enough storage is reserved to hold the largest possible

variant of the recordin this case, pair of integers.


However, suppose we know that the node will only contain a single integer. Then
it is wasteful to reserve storage for two integers, since only one is necessary. To assist
in conserving storage, Pascal allows another from of the new and dispose procedures.
new(p, single) allocates a variable of type nodetype with only enough storage in the
variant part of the record to contain a single integer, while new(p, pair) allocates a
nodetype variable with enough storage in the variant part to contain two integers. The
tag field sp is not assigned a value by the call to new. Subsequently, the tag field may
only be assigned the value specified in the call to new and may not be changed. or its
entire lifetime, such a record adopts a single one of its variants
! node created by the new function with two arguments must be freed with the
dispose function using two arguments. dispose(p, single) is used to free a record
created by new(p, single), while dispose(p, pair) is used to free a record created by
new(p, pair). Similarly, if a variant record has more than one tag field "in the case of a
variant record which contains a field that is itself declared as a variant record#, new
and dispose can be called with more than two arguments.
!nother restriction on a variant record variable created by the new function with
more than one argument is that it cannot appear in an assignment statement. The
reason for this is that an assignment statement is usually implemented by copying the
contents of memory from one area to another. The amount of memory to be copied is
determined from the types of the variable in the assignment. $hen a variant record is
allocated with a specified tag value, it is not always possible to determine in advance
"i.e., at compilation time# the si%e of the record and, therefore, the amount of memory
to be copied. !lthough the source and the target of the assignment might be of the
same type, they may be of different si%es since one might be allocated with a specific
tag value "two parameters in the new function# and the other might be allocated with a
different tag value r without any specific tag value "only one parameter in the new
function#. or this reason, standard Pascal prohibits such record assignments.
&ndividual Pascal compilers might implement such assignments consistently
'correctly,( inconsistently 'correctly,( consistently,( or not at all. Such assignments
are therefore not recommended even if, on occasion, your complier does allow the
assignment.
)f course, individual fields of such variant record, including those fields present
only in the allocated variant, may be assigned freely, sub*ect only to the general
Pascal type+agreement rules for assignment.
Comparing the Dynamic and Array
Implementations of Lists
&t is instructive to e,amine the advantages and disadvantages of the dynamic and
array implementations of linked lists. The ma*or disadvantage

Das könnte Ihnen auch gefallen