Putting Mega-Byte Memory to Work

by Thomas M. Lahey


In today’s programming world, memory size (really, memory available to execute a program) is no longer a consideration. This statement is based on two facts:

    • 128 megabytes of memory costs $25; and
    • Operating systems provide virtual memory.

It’s true virtual memory affects execution time, but if that is a problem, buy some of that affordable memory!

The most important consequence of memory size no longer being a constraint:

Applications should be coded using techniques that
make the program able to process data sets of any size.

Aside: Because memory is available, combine programs from the past that had to read in the data, do some calculations, write the data to a file, chain to the next program, read in the data, …. The payoff will be impressive.

The following code outlines one technique (linked list, a.k.a., thread) for making a program independent of the size of a user’s data set.

!  Declare structure:
TYPE  ::  Derived_Type
   TYPE(Derived_Type), POINTER  ::  Next     !  Link list pointer
   ...                                       !  Other components
END TYPE Derived_Type

!  Declare variables:
TYPE(Derived_Type), POINTER  ::  Head, Tail, Current
...                                          !  Other declarations

!  Initialize linked list & process first element
ALLOCATE( Head )      !  Head is ALWAYS list's beginning
Tail => Head          !  Tail is ALWAYS list's last element
...                   !  Code to define Head component(s)

!  Loop; create list; count members
DO  Number_in_List = 1, HUGE( 1 )  
   ALLOCATE( Current )
   READ(Unit_No, FORMAT, IOSTAT=iostatus) Current%Component
   IF( iostatus /= 0 ) EXIT       !  Hopefully EOF, not error
   ...                            !  Code defines Current component(s)
   Current%Next => NULL( )        !  NULL terminates thread
   Tail%Next => Current           !  Thread Current
   Tail => Current                !  Tail points to last element
END DO                            !  Loop to create & read next line

...                               !  Data set read, begin to process


Each time it is necessary to loop through the code, either of the following fragments will work as the outermost control structure:

Current => Head
DO  Number_in_List = 1, Number_in_List
   ...             !  Process link elements
   Current => Current%Next
Number_in_List = Number_in_List – 1


Current => Head
DO WHILE( ASSOCIATED( Current%Next ) )
   ...             !  Process link elements
   Current => Current%Next


Note: The above loops replace indexed DO loops
(possible more than one DO in a nest) that were used
to move through the application’s array-elements.