Скачать Clear Memory to a Fixed Value at the Program Start

Скачать файл (1,55 Кб)

CLEARMEM - A Turbo Pascal unit to automatically initialize the heap, stack, or data segment to a fixed value.

Written by D.J. Murdoch for the public domain.


    filler : byte = 0;

This byte is used as the initial value. A good choice for turning up uninitialized variables is $FF - this will often cause a range check, and will cause runtime error 207 if you try to use an uninitialized single, double or extended.

procedure clear_heap;

This procedure fills the heap with filler bytes. Automatically called in the initialization section.

procedure clear_globals;

This procedure fills all global variables (except those in the system unit) with filler bytes. Very dangerous! *Not* called in the initialization section (unless you change it). Written for TP 6.0; the source code gives hints on how to change it for other versions.

procedure clear_stack;

his procedure fills the unused part of the stack with filler bytes.


It's safe to call clear_heap any time; it'll fill all free blocks of 6 bytes or more on the heap with the filler byte. It won't necessarily do a perfect fill if the heap is fragmented, because the free list will overwrite the filler.

It's also safe to call clear_stack any time, but is a bit less effective. Any interrupts that happen after your call will mess up the stack that you've just cleared, so local variables won't necessarily be properly initialized. It doesn't touch anything already allocated.

It's definitely *NOT* safe to call clear_globals any time except at the very beginning of your program, and only then from the initialization section of this unit, and only if this is the very first unit that you Use in the main program.