Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CREATE and VARIABLE

Tags:

forth

gforth

What is the difference between the two words CREATE and VARIABLE?

For example, in this code:

VARIABLE MYARRAY 2 CELLS ALLOT
170 340 220 MYARRAY ! MYARRAY 1 CELLS + ! MYARRAY 2 CELLS + !

CREATE MYARRAY 220 , 340 , 170 ,

In both cases the result is the same in memory. Is it right? When should I use one in place of the other? And why?

like image 935
user1018183 Avatar asked Oct 12 '25 15:10

user1018183


1 Answers

Right, VARIABLE allocates one cell of memory. Also, a standard program may not assume that it can add data after the variable, see Contiguous regions.

CREATE does not by itself allocate any memory, but starts a contiguous region which can be extended. And, it's the only word which a standard program may use in combination with DOES>.

The difference may be mostly stylistic (unless you use DOES> or care deeply about the standard). It's more clear to a reader that VARIABLE is a plain variable. CREATE can be any type of data structure.

If you want an initialised variable, you can use either VARIABLE FOO BAR FOO ! or CREATE FOO BAR ,. I don't see that either alternative is preferred over the other to a significant degree.

like image 93
Lars Brinkhoff Avatar answered Oct 15 '25 15:10

Lars Brinkhoff