Lists and typed lists are usually static data, but
NEW can be used to create dynamically allocated versions.
This form of
NEW can be used only as an expression, and it takes the list (or typed list) as its argument and returns the address of the dynamically allocated copy of the list.
Deallocation of the memory allocated in this way is a bit more complicated than before, but you can, of course, let it be deallocated automatically at the end of the program.
The following example shows how simple it is to use
NEW to cure the static data problem described previously (see 9.5.7 Static data).
The difference from the original, incorrect program is very subtle.
PROC main() DEF i, a:ARRAY OF LONG, p:PTR TO LONG FOR i:=0 TO 9 a[i]:=NEW [1, i, i*i] /* a[i] is now dynamically allocated */ ENDFOR FOR i:=0 TO 9 p:=a[i] WriteF('a[\d] is an array at address \d\n', i, p) WriteF(' and the second element is \d\n', p) ENDFOR ENDPROC
The minor alteration is to prefix the list with
NEW, thereby making the list dynamic.
This means that each
a[i] is now a different list, rather than the same, static list of the original version of the program.
Typed lists are allocated in a similar way, and the following example also shows how to deallocate this memory.
Basically, you need to know how long the new array is (i.e., how many elements there are), since a typed list is really just an initialised array.
You can then deallocate it like a normal array, remembering to use an appropriately typed pointer.
Object-typed lists are restricted (when used with
NEW) to an array of at most one object, so is useful only for allocating an initialised object (not really an array).
Notice how, in the following code, the pointer
q can be treated both as an object and as an array of one object (see 9.4.2 Element selection and element types).
OBJECT myobj x:INT, y:LONG, z:INT ENDOBJECT PROC main() DEF p:PTR TO INT, q:PTR TO myobj p:=NEW [1, 9, 3, 7, 6]:INT q:=NEW [1, 2]:myobj WriteF('Last element in array p is \d\n', p) WriteF('Object q is x=\d, y=\d, z=\d\n', q.x, q.y, q.z) WriteF('Array q is q.x=\d, q.y=\d, q.z=\d\n', q.x, q.y, q.z) END p, q ENDPROC
The dynamically allocated version of an object-typed list differs from the static version in another way: it always has memory allocated for a whole number of objects, so a partially initialised object is padded with zero elements. The static version does not allocate this extra padding, so you must be careful not to access any element beyond those mentioned in the list.
The deallocation of
NEW copies of normal lists can, as ever, be left to be done automatically at the end of the program.
If you want to deallocate them before this time you must use the function
FastDisposeList, passing the address of the list as the only argument.
You must not use
END or any other method of deallocation.
FastDisposeList is the only safe way of deallocating lists allocated using
Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.