How far can stack [LIFO] solve do automatic garbage collection and prevent memory leak ?

Elizabeth D Rather erather at forth.com
Mon Aug 16 14:09:57 EDT 2010


On 8/15/10 10:33 PM, Standish P wrote:
...

I don't understand a lot of your post (and it's clear that I'm not 
alone).  I don't know whether it's a (human) language problem or simply 
an issue of your having read too many books and not having enough 
practical experience, but at least I can try to address the Forth questions.

>>> If Forth is a general processing language based on stack, is it
>>> possible to convert any and all algorithms to stack based ones and
>>> thus avoid memory leaks since a pop automatically releases memory when
>>> free is an intrinsic part of it.

Forth uses two stacks.  The "data stack" is used for passing parameters 
between subroutines ("words") and is completely under the control of the 
programmer.  Words expect parameters on this stack; they remove them, 
and leave only explicit results.  The "return stack" is used primarily 
for return addresses when words are called, although it is also 
available for auxiliary uses under guidelines which respect the primary 
use for return addresses.

Although implementations vary, in most Forths stacks grow from a fixed 
point (one for each stack) into otherwise-unused memory.  The space 
involved is allocated when the program is launched, and is not managed 
as a heap and allocated or deallocated by any complicated mechanism.  On 
multitasking Forth systems, each task has its own stacks.  Where 
floating point is implemented (Forth's native arithmetic is 
integer-based), there is usually a separate stack for floats, to take 
advantage of hardware FP stacks.

>>     - is forth a general purpose language? Yes
>>     - are all algorithms stack based? No
>
> Does Forth uses stack for all algorithms ? Does it use pointers , ie
> indirect addressing ? If it can/must use stack then every algorithm
> could be made stack based.

Forth uses its data stack for parameter passing and storage of temporary 
values.  It is also possible to define variables, strings, and arrays in 
memory, in which case their addresses may be passed on the data stack.

Forth is architecturally very simple.  Memory allocations for variables, 
etc., are normally static, although some implementations include 
facilities for heaps as needed by applications.

Hope this helps.  If you are interested in learning more about Forth, 
there are several books available (try Amazon).  Get one of the more 
recent ones, some of which I wrote.

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."
==================================================



More information about the Python-list mailing list