[comp.lang.fortran] We need pointers

cik@l.cc.purdue.edu (Herman Rubin) (09/25/88)

Consider the following situation:  You are using the elements of a buffer.
There is no reasonable way that you can know in advance how many elements
there are in the buffer, or how many you will need to use.  You have a
program to refill this buffer.  In fact, you have many buffers and many
refill programs, and it is possible to keep track of which one you are
using.

Now the way that I do this in C is to have for each buffer a descriptor
bufdes which is a pointer to an array of length at least two, one element
of which is the buffer endpoint and another is the current pointer location.
Other elements of the buffer may contain refill information.  Also, I have
a pointer to the pointer to the refill function (it may even be an element of
bufdes).  The way I would use the buffer (there are other equally good ways,
no flames please) is 

	If the buffer is empty, call *bufrefill(bufdes);
(that means, call the function whose address is stored in the location
bufrefill with the argument the descriptor bufdes.)

	x = *(--bufdes[0]);
(decrease the pointer and read it).

If the array is fixed, the pointer can be replaced by an index, but not if
the array is variable.  The same holds for the decision process and the call.

Another advantage of the pointer notation is that the block to which bufdes
points is a COMMON block, whose size must be established somewhere, but the
programs calling it do not need to know the size.  Pointers give us a way to
transmit which array a subroutine needs, without specifying anything about the
array.  I have used this device in Fortran with difficulty for handling the
various kinds of random variables needed in a simulation, and I can think of
no reasonable alternative.  Of course, the refill routines were not Fortran.
-- 
Herman Rubin, Dept. of Statistics, Purdue Univ., West Lafayette IN47907
Phone: (317)494-6054
hrubin@l.cc.purdue.edu (Internet, bitnet, UUCP)