Search code examples
memoryforthgforth

Does CREATE waste space?


In gforth, CREATE, by itself, creates a variable, which takes some memory space: a few bytes to store the value plus two instructions to put the address on the stack and return:

create a  ok
42 a !  ok
a @ . 42  ok

If instead of a variable you want to create an 80-byte buffer, you can write

: TAMPON CREATE 80 ALLOT DOES> + ;

but those 80 bytes would come after the bytes taken by CREATE for the default variable, which is a waste.

Does CREATE waste space or am I missing something?


Solution

  • CREATE does not waste space. It "does not allocate data space in name's data field" (see 6.1.1000 CREATE).

    This example shows incorrect usage:

    create a
    42 a !
    a @ .
    

    At this moment, a and here returns the same address:

    a here = . \ it prints "-1"
    

    So you have to reserve required space before use:

    1 cells allot
    a cell+ here = . \ it prints "-1"