Basic tools for allocating memory

The words create and allot are the basic tools for setting aside memory and attaching a convenient label to it. For example, the following transcript shows a new dictionary entry x being created and an extra 16 bytes of memory being allotted to it.
create x  ok<$,ram>
x u. f178  ok<$,ram>
here u. f178  ok<$,ram>
10 allot  ok<$,ram>
here u. f188  ok<$,ram>
When executed, the word x will push the address of the first entry in its allotted memory space onto the stack. The word u. prints an unsigned representation of a number and the word here returns the address of the next available space in memory. In the example above, it starts with the same value as x but is incremented by (decimal) sixteen when we allotted the memory.


We can now access the memory allotted to x using the fetch and store words discussed earlier, in Section6. To compute the address of the third byte allotted to x we could say x 2 +, remembering that indices start at 0.

30 x 2 + c!  ok<$,ram>
x 2 + c@  ok<$,ram>30
We will discuss a way to neatly package the snippets of code required to do the address calculation later, in Section12.2. Finally, note that the memory context for this example has been the static RAM, however, (as shown for variables in Section6.1) the context for allotting the memory can be changed.


Peter Jacobs 2013-06-12