[futurebasic] Re: XREF

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 1997 : Group Archive : Group : All Groups

From: Rick Brown <rbrown@...>
Date: Tue, 11 Nov 1997 13:58:33 -0600
David Cottrell wrote:

> globals
>   DIM MYmean!
>   DIM 63 MYString$
> _bazillion      =1000000
> _realMax        =1000
> XREF @ gList.MyRec(_bazillion)
> ---------------------------------------------------
> LOCAL FN enterData
> DIM myArray.Top&
> XREF@ myArray.MyRec(_realMax)
> myArray.Top& = gList.Top&
> FOR x = 1 TO 3
>         myArray.MYmean!(x)      = x + 0.001     'crash big time here!!!
>         myArray.MYString$(x)   = "The String"
> NEXT x
> 'FN printData
> err% = FN DISPOSHANDLE (myArray.Top&)             'trash handle when done
> fn to set up handle:
> LOCAL FN setUpData
>   gList.Top& = FN NEWHANDLE (3 * _MyRec)
> fn to clean up after
> LOCAL FN trashData
>   err% = FN DISPOSHANDLE (gList.Top&)             'trash handle when done

First, I would get rid of the "XREF @ gList.MyRec(_bazillion)" statement
that appears in your globals section.  It isn't doing anything.  A
statement such as:

  XREF@ myArray.myRecord(_someNumber)

essentially means: "Whenever a reference to the array called 'myArray'
appears elsewhere in this LOCAL FN (or in "main", if that's where the
XREF is), then look for a long integer called 'myArray&', and interpret
its value as a handle to a block containing the array data."  In light
of the 'myArray.Top&' workaround, FB is probably actually looking for a
_short_ integer called 'myArray%' which contains the handle (you need to
say "DIM myArray%;4" in order to allocate enough bytes for a 2-byte
integer to "contain" a 4-byte handle).

At any rate, the "XREF @ gList.MyRec(_bazillion)" statement does
nothing, because it's only meaningful if there's a "DIM gList&" or a
"DIM gList%;4" statement somewhere along with it, to define gList& (or
gList%) as a global variable, which is to contain the handle.  In
particular, XREF@ doesn't reserve any space for anything.

Now let's look inside your local FN:

>LOCAL FN enterData
>DIM myArray.Top&

'atsa no good.  The "dot" syntax means something different when it's
used in a DIM statement than when it's used elsewhere.  "DIM
myArray.Top&" means: "Evaluate the constant "_top" as an integer, then
reserve that many bytes for the variable "myArray".  But since _top
equals zero, what you're doing is reserving zero bytes!  The "&" at the
end is ignored in this case.

On the other hand, when "myArray.Top&" is used elsewhere, it means: "the
4-byte integer which is located '_top' bytes past the address of the
variable 'myArray'."  And since _top = 0, this translates to: "the
4-byte integer which is locate _at_ the address of the variable
'myArray'."  But you've reserved zero bytes for myArray, so when you try
to store 4 bytes into it, you are surely clobbering other variables. 
Instead of "DIM myArray.Top&", do this: "DIM myArray;4"

>XREF@ myArray.MyRec(_realMax)

This is okay.  Recall that it means: "Whenever an array called 'myArray'
is referenced elsewhere in this LOCAL FN, look for a _variable_ called
'myArray', find the 4 bytes in it, and interpret those bytes as a handle
to a block which contains the array data."

>myArray.Top& = gList.Top&

What you should be doing here is assigning a handle to myArray.Top&. 
However, that's not working, because there is no handle in gList.Top&. 
First of all, gList is interpreted as a _local_ variable (you need to do
"DIM gList;4" in the globals section to make it global).  So this
statement just assigns garbage to myArray.Top&

>FOR x = 1 TO 3
>        myArray.MYmean!(x)      = x + 0.001     'crash big time here!!!
>        myArray.MYString$(x)   = "The String"

Probably this is crashing because FB is looking in myArray.Top& for a
handle to the array block, but myArray.Top& does not contain a valid
handle (as explained above).

>fn to set up handle:
>LOCAL FN setUpData
>  gList.Top& = FN NEWHANDLE (3 * _MyRec)

Again, gList is a local variable, so the "gList" in this FN is not the
same "gList" in FN enterData.

Try making these changes to your program:

In the globals section, change this:
  XREF @ gList.MyRec(_bazillion)
...to this:
  DIM gList;4

In FN enterdata, change this:
  DIM myArray.Top&
...to this:
  DIM myArray;4

Hope this helps.
- Rick