POINTERS, HANDLES AND THE TOOLBOX POINTERS Although the stack pointer is a case in which the microprocessor is actually storing information - the address of the top of the stack - you should know now that pointers stored in regular RAM play an important role in programming IIGS tools. We'll see pointers in action in later chapters. Let's examine the components of a pointer. Since a pointer is an address to a memory location in a particular memory bank, you will need 4 bytes of memory for the pointer. Two bytes - the low, or rightmost, bytes of a four-byte number - refer to the memory location. Two high bytes refer to the bank number. Therefore, a pointer to address $3F02 in bank $03 will have to be placed in memory as the 4-byte number $00033F02. If hex numbers, such as this one, ever get difficult to read, simply divide them into single bytes, in this case $00 $03 $3F $02, or two-byte chunks, $0003 $3F02. This pointer mechanism is illustrated in Figure 2-16. Pointers have to be used with care, however. In an application, it is quite possible for data portions to be moved around a bit. This would happen when a block of memory is no longer needed by the program, and all remaining blocks that can be moved (i.e., that were created as relocatable blocks) are compacted together. The net effect of memory compaction is to open up large, empty blocks for the program to use for other purposes. But if your program was pointing to a specific chunk of data in memory, it will lose track of the data when the data block moves during compaction. HANDLES Fortunately, there is a way around this dilemma. A common technique in IIGS programming is to use a handle instead of a pointer - indeed, many tools require the use of handles rather than pointers. Instead of using a pointer to refer to a specific data address in memory, a handle is a pointer to a master pointer, whose location never changes. The master pointer, in turn, keeps track of the location of the desired chunk of data as the data shifts around memory while a program runs. Figure 2-17 demonstrates the stages involved here, and compares the result of using a handle instead of a pointer. At first a handle might seem like a long way to go to keep track of a block of data, but because the master pointer never moves in memory, the IIGS toolbox can always find it and supply it with new information about the location of relocatable data. Since even the program that calls the handle may move during a memory compaction, the master pointer system is far more efficient than if each pointer in a program tried to track relocatable data on its own. Notice another important matter, one that has to do with memory notation, rather than pointers and handles. In the last several figures, we've been displaying varying length items as simple blocks of memory in these vertical memory maps. The blocks are not necessarily drawn to scale, and a block can contain a chunk of information ranging from a single byte to perhaps thousands of bytes. Maps are designed to give you a bird's eye view of items stored in memory at a given instant. Therefore, it is important that you watch the words used to describe information in any block of data. If the wording in one box indicates it holds a pointer or a handle, then you know that the box represents a total of 4 bytes from memory. As you start to work with the tools, you'll quickly become versed in the amount of memory each type of item requires.