[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: Sat, 6 Mar 1999 11:39:58 +1300
Pierre Zippi wrote:
>I changed _max to 5000 and s$ to 100 chars.
>I ran it uncompiled in FBII with 18000K allocated.
>When I ran the modified code, during& went negative.
>As I decrease the size of _max, during& approaches zero then goes positive.
>What does that mean?

When FN STACKSPACE is negative it means that you
(a) have attempted to use more stack than the system has reserved, and
(b) are a hair's-breadth away from a crash.
The only reason that your program didn't crash was that the array
(illegally spilling over the top of the stack) was never accessed in FN
Test&. The modified FN Test& below shows how to crash into
BowelsOfTheMemoryManager.
Conclusion: don't use the stack for a large array. Either globally DIM it,
or use relocatable memory (FN NEWPTR and XREF, or FN NEWHANDLE and XREF@).

_max=5000
LOCAL FN Test&
 DIM 100 s$ (_max)
 LONG IF FN STACKSPACE<100
  INPUT "OK to crash?"; ok$
  IF UCASE$(ok$)<>"Y" THEN END
 END IF
 FOR j=0 TO _max
  s$(j)=""
 NEXT
END FN=FN STACKSPACE

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

Robert