gforth
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [gforth] Tutorial - Memory Section - 3 issues


From: Josh Grams
Subject: Re: [gforth] Tutorial - Memory Section - 3 issues
Date: Sun, 7 Mar 2010 15:15:54 -0500
User-agent: Mutt/1.5.18 (2008-05-17)

>Issue One -  CREATE does not push an address onto the stack, yet ALLOT
>seems to know where to start allocating cells...

Most memory in Forth is sequentially allocated from a big pool, and
never released.  HERE returns the value of the first free memory
location.  Everything before that is used, everything there or after is
available.

ALLOT simply adds its argument to the value of HERE, allocating memory
for a positive argument, or (rarely) freeing memory for a negative
argument.

CREATE defines a word which returns the current value of HERE.

>Issue Two - It is a fact that both VARIABLE and CREATE create words
>which are simply the address of a memory location.
>
>However, the docs confuse this fact, by stating that VARIABLE creates
>global variables while CREATE creates a word.
>I dont think it helps a beginner to use the term global variable and
>word interchangeably. Because they start thinking there are two
>different programmatic constructs when they are not.

In Forth, *everything* is a word.  Terms like "global variable" or
"constant" or "array" or whatever are just convenient descriptions for
certain kinds of words.  Don't take them too seriously. :)

>Issue Three - How does VARIABLE differ from CREATE?

VARIABLE defines a word which returns the address of one cell of memory.

CREATE defines a word which returns the current value of HERE; then the
programmer can however much memory is required for that word.

VARIABLE is approximately:

        : VARIABLE ( "name" -- )  CREATE  1 CELLS ALLOT ;

But note that VARIABLE does not guarantee that the cell is right before
HERE.  You may not be able to use ALLOT to "make a variable bigger".

Note also that once you define the next word, you can no longer expand
the space available to a CREATEd word.  See the standard, section
3.3.3.2 "Contiguous Regions" for details.

http://www.taygeta.com/forth/dpans3.htm#3.3.3.2

--Josh




reply via email to

[Prev in Thread] Current Thread [Next in Thread]