Re: Need some help understanding array definitions



m_l_g3 wrote:
On Jun 10, 1:51 am, Elizabeth D Rather <erat...@xxxxxxxxx> wrote:

So, you don't want to use a defining word inside a colon definition to
*define* a data object,

I want to explain one more thing.

In Forth, VARIABLE defines a global variable.
Analogously, all other defining words define *global*
things (procedures, arrays, etc.)

As in any programming language, it would be a mistake to use
global variables where you need local ones.

Data structures defined with VARIABLE, CREATE, VALUE, CONSTANT, and related words (as well as custom data structures such as ARRAY) are, indeed, all global. Unlike some languages, Forth doesn't discourage defining global data structures, but it's important to understand their proper use. They provide for "persistent" data (that is, data that will be used over some extent of time or by a number of different definitions), as well as space for strings and arrays. Temporary or intermediate values, as m_l_g says, are appropriately kept on the stack. Strings and arrays should be in defined data structures and referenced by address and length (in the case of strings) or address (in the case of arrays) on the stack.

So what about local variables?

They are not needed. Local variables are used to keep values
that exist while the procedure executes. But you do not
necessarily need local variables for this, you can keep
the values on the data stack. (Well, in fact, whether or nor
stack is enough depends on the *number* of values that you need
access to.)

So, if you study Forth, do not use variables, global or local,
use values on the stack where possible.

This is sounding a little more absolute than is appropriate, I think. The newbie Forth programmer tends to define too many variables, because the stack is unfamiliar and uncomfortable. Therefore, we try to encourage stack use. But there are definitely times when defining a variable or data structure will simplify your code, and then you shouldn't hesitate.

I don't recommend that Forth newbies use local variables. In the first place, their implementation varies across different versions of Forth (there is a standard, but many implementors do it differently). In the second place, it can become a crutch that keeps you from learning good factoring and stack management skills.

Cheers,
Elizabeth

--
==================================================
Elizabeth D. Rather (US & Canada) 800-55-FORTH
FORTH Inc. +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================
.



Relevant Pages

  • Re: ALLOCATABLE arrays
    ... || If try to create a very large array on the stack and you do not have enough ... || Allocating on the heap gives you access to a hell of a lot more memory (well ... and "heap" (and there probably are/were some computers which don't/didn't ... | Automatic arrays are always allocated on the stack. ...
    (comp.lang.fortran)
  • Re: Fortran memory allocation (stack/heap) issues
    ... > rather than Fortran, ... dynamic allocation, and relatively little stack allocation. ... value return and arrays by reference. ...
    (comp.lang.fortran)
  • Pratap PattnaiK - IBM Fellow
    ... Dr. Pratap Pattnaik has been appointed an IBM Fellow for his technical ... leadership in defining and optimizing the stack for IBM's enterprise server ... He has been the chief architect and scientist for defining, ... Pattnaik's contributions span across the enterprise stack of systems, ...
    (bit.listserv.ibm-main)
  • Re: heap allocation of arrays
    ... | to force all arrays to be allocated on the heap. ... | the stack would be replaced with pointers on the stack. ... | heap is easier to detect than failure to allocate space on the ...
    (comp.lang.fortran)
  • Re: ALLOCATABLE arrays
    ... > If try to create a very large array on the stack and you do not have enough ... > Allocating on the heap gives you access to a hell of a lot more memory (well ... Allocatable arrays are allocated on the heap. ... Automatic arrays are always allocated on the stack. ...
    (comp.lang.fortran)