Is it okay to use dictionary memory without 'allot'?

222 Views Asked by At

I am doing a programming exercise where I'm trying to do the same thing in different ways. (I happen to be adding two 3 element vectors together in Forth). In one of my revisions I used the return stack to store temporary values (so I am using that feature), but in addition to that I am considering using un-allocated memory as temporary storage.

I created two words to access this memory:

: front! here + ! ;
: front@ here + @ ;

I tried it in my experiment, and it seemed to work for what I was doing. I don't have any intention to use this memory after my routines are done. And I am living in dictionary, of which memory has already been given to the program.

But, my gut still tells me that this is a bad thing to do. Is this such a bad thing?

If it matters, I'm using Gforth.

3

There are 3 best solutions below

0
On BEST ANSWER

Language-lawyer strictly speaking, no. ANS Forth 3.3.3.2 states:

A program may perform address arithmetic within contiguously allocated regions.

You are performing address arithmetic outside any allocated region.

However, it might be perfectly fine in some particular implementation. Such as gforth.

Note that there is a word called PAD, which returns an address to a temporary memory region.

2
On

: front! here + ! ;

What's the stack diagram? I guess ( n offset_in_cells -- )?

0
On

It's okay if you know what you are doing, bud PAD is a better place than HERE to do it. There is also the alternative ALLOCATE and FREE:

ALLOCATE ( u -- a-addr ior )

Allocate u address units of contiguous data space. The data-space pointer is unaffected by this operation. The initial content of the allocated space is undefined.

If the allocation succeeds, a-addr is the aligned starting address of the allocated space and ior is zero.

If the operation fails, a-addr does not represent a valid address and ior is the implementation-defined I/O result code.

FREE ( a-addr -- ior )

Return the contiguous region of data space indicated by a-addr to the system for later allocation. a-addr shall indicate a region of data space that was previously obtained by ALLOCATE or RESIZE. The data-space pointer is unaffected by this operation.

If the operation succeeds, ior is zero. If the operation fails, ior is the implementation-defined I/O result code. American National Standard for Information Systems