[futurebasic] Re: [FB] Local DIM crash Global not

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : March 1999 : Group Archive : Group : All Groups

From: Robert Purves <robert.purves@...>
Date: Fri, 5 Mar 1999 18:43:33 +1300
>In the old app I use:
>Clear local
>DIM sorted$(_maxRow)
>LOCAL FN AlphaSort
>...
>This causes the new app to crash.

Local variables, including arrays, are allocated on the stack, which is
generally _much_ smaller than the heap. A very large local array will cause
the stack to collide with the heap, with a crash.

How much stack space can you safely use? The space available turns out to
depend on the compiled application's Get Info memory allocation.

_max=31
LOCAL FN Test&
 DIM s$(_max) 'allocate (_max+1)*256 bytes + 12 bytes for a LOCAL FN
END FN=FN STACKSPACE

WINDOW 1
before& = FN STACKSPACE
during& = FN Test&
after&  = FN STACKSPACE
PRINT before& "before"
PRINT during& "during"
PRINT after& "after"
PRINT "Used by FN Test&=" before&-during&
DO
UNTIL FN BUTTON

Investigation with a compiled app from the above program, and various
memory allocations (in the OS 8.5 Get Info window) shows that FB2 sets the
"before" stack as:-

    initial_stack_size = min (32440, 1400 + 32*Kpartition)

where Kpartition is the number of K entered.

For example with 8000K the stack is a huge 257400 bytes. With 100K the
stack is only 32440 bytes, and a crash should be expected if _max > = 126.

With a global DIM, the memory is taken from the application's heap.

Robert