[futurebasic] Re: Function Pointers

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 1998 : Group Archive : Group : All Groups

From: tedd <tedd@...>
Date: Thu, 28 May 1998 10:01:11 -0400
Mark:

> Can you give a very simple explanation/example of how a "linked list"
>works? I
>still can't figure it out.
>
>"BASICally challenged"
>Mark


Mark:

A linked list is nothing more than list of variables that are stored in
memory in a special way.

Normally, no one has a problem with the statement a$ = "hello". This just
an assignment of the string "hello" into the variable a$. However, if one
looks closer to this relationship, the variable "a$" is actually a place in
memory somewhere. It has a starting place in memory and occupies memory for
the length of the string. To find out where in memory the variable is, one
would dereference the variable a$ by using

   placeInMemory& = @a$

In other words, the text "hello" starts at an address that is shown by the
variable "placeInMemory&". Let's say that the value for "placeInMemory" is
23123. If so, then the text "hello" starts at 23123 and continues for the
length of the string. In this case 5 bytes.

Now, if you know the where in memory a variable is, then you don't
necessarily need to access it by using a$. You could use b$ =
[placeInMemory&], which is the same thing as b$ = a$ except that you used
the memory address of "a$" instead of the variable "a$".

So now, as demonstrated, we can use the memory address of a variable to get
the contents of memory instead of using the variable name. What does this
do for us? Well... it gives us another method of accessing variables.

In the case of using linked lists, it is just a method of using records
such that it includes the address of the next record (a single linked list)
as a reference to the next set of variables. For example, if I wanted to
have a record (a set of similar variables), I could set up a record like
this:

DIM RECORD linkedList
  DIM next&
  DIM a$
  'as many variables as I want...
END RECORD _linkedListSize

Where a$ is the variable I've been using and next& is the address of the
next *record*. In this way, each record not only provides me with the
variables I want, but also provides the address in memory where I can find
the next set of variables. So, if I have the address of the record, then I
can find the address of any member of the record.

For a real example of linked list, look to the FBwebring for my "tedd's
linkedList" example. That demo will show you how to set up a linked list,
allocate memory for a linked list and access each member of the linked list.

Now, if you follow that example, then you can also imagine something more
complex by not only placing variables into memory via a linked list, but
also placing the address of functions into a linked list.

Now why would anyone want to place a function's address into a linked list?
Good question. But as I found in a program I wrote recently (namely a
Street Atlas type program) I needed to set up a system where I had
functions that would act upon variables at a specific time that I could not
predict. In other words, some linked lists would be necessary for the
program to show it's data and other weren't. However, I had no idea before
time.

As such, I developed a linked list system that held the addressees of the
functions and a list of the variables that the functions would operate
upon. It was confusing, I must admit. For example, I had four sets of data
(each set containing tens of thousands of records) which were divided into
255 different record types which were separated into 4 sets of 40 different
linked list that was operated upon by 255 different functions and each set
was activated by a single reference. A major task for me, but a minor task
for FutureBASIC. The language never fails to impress me.


tedd




___________________________________________________________________
<mailto:tedd@...>	               http://sperling.com/