Next: , Previous: Return Stack Tutorial, Up: Tutorial


3.23 Memory

You can create a global variable v with

     variable v ( -- addr )

v pushes the address of a cell in memory on the stack. This cell was reserved by variable. You can use ! (store) to store values into this cell and @ (fetch) to load the value from the stack into memory:

     v .
     5 v ! .s
     v @ .

You can see a raw dump of memory with dump:

     v 1 cells .s dump

Cells ( n1 -- n2 ) gives you the number of bytes (or, more generally, address units (aus)) that n1 cells occupy. You can also reserve more memory:

     create v2 20 cells allot
     v2 20 cells dump

creates a word v2 and reserves 20 uninitialized cells; the address pushed by v2 points to the start of these 20 cells. You can use address arithmetic to access these cells:

     3 v2 5 cells + !
     v2 20 cells dump

You can reserve and initialize memory with ,:

     create v3
       5 , 4 , 3 , 2 , 1 ,
     v3 @ .
     v3 cell+ @ .
     v3 2 cells + @ .
     v3 5 cells dump
Assignment: Write a definition vsum ( addr u -- n ) that computes the sum of u cells, with the first of these cells at addr, the next one at addr cell+ etc.

You can also reserve memory without creating a new word:

     here 10 cells allot .
     here .

Here pushes the start address of the memory area. You should store it somewhere, or you will have a hard time finding the memory area again.

Allot manages dictionary memory. The dictionary memory contains the system's data structures for words etc. on Gforth and most other Forth systems. It is managed like a stack: You can free the memory that you have just alloted with

     -10 cells allot
     here .

Note that you cannot do this if you have created a new word in the meantime (because then your alloted memory is no longer on the top of the dictionary “stack”).

Alternatively, you can use allocate and free which allow freeing memory in any order:

     10 cells allocate throw .s
     20 cells allocate throw .s
     swap
     free throw
     free throw

The throws deal with errors (e.g., out of memory).

And there is also a garbage collector, which eliminates the need to free memory explicitly.

Reference: Memory.