Navigation bar
  Start Previous page
 91 of 156 
Next page End 86 87 88 89 90 91 92 93 94 95 96  

85
must be used with great care because careless or improper use can result in malfunctioning of the heap. 
There are two fundamental problems that can arise: heap exhaustion and heap corruption.
If blocks of memory are allocated from the heap and never freed, the heap will eventually become
exhausted and subsequent allocation requests will fail.  Since String variable types rely on proper heap
function, assigning values to String variables will not have the desired effect when the heap is exhausted.
Heap corruption occurs when the heap management data structures are inadvertently modified.  Such
corruption can result from writing to a previously allocated block after it has been freed or writing to
memory outside the bounds of a properly allocated but not yet freed block.  The heap may also be
corrupted by inadvertent writes to RAM within the bounds of the entire heap such as might result from the
careless use of the RamPoke() or BlockMove() routines, by writing beyond the bounds of an array, or
overflowing the stack assigned to a task.  Heap corruption will also be the likely result of passing an
invalid value to System.Free().  The only values that may be safely passed to System.Free() are those
that have been returned by System.Alloc() but have not yet been passed to SystemFree().  As a special
case, passing the value zero to SystemFree() is benign.
The value Register.HeapEnd, described in this document, may be useful for monitoring the state of the
heap.  For additional information, see the descriptions of System.Alloc() and System.Free() in the ZBasic
System Library Reference manual.  Also, for native mode devices (e.g. the ZX-24n) the function
System.HeapHeadRoom() may be used to determine, at any time, the amount of unused space that
remains in the heap.  Moreover, for native mode devices the directive Option HeapSize can be used to
specify an upper limit on the size of the heap thereby preventing it from encroaching on the task stacks.
Previous page Top Next page