F90 Model : Pointers and Advanced Memory Management

Fortran 90 provides advanced dynamic memory management on par with `C'. Therefore, many of the common `C' data structures can be translated into equivalent Fortran 90 forms. This example shows how to implement a singularly-linked list, where we anchor the first element to head and add successive elements until finished. All access to this data is in the same order as input. This method is ideal for situations where the number of elements is known at run-time, but only after the list is entered. Any element can be accessed in linear time, hence, random access is discouraged.

Each element is a user-derived type that can contain other user-derived types, intrinsic types, and, most importantly, a pointer of the element type. The last element's pointer will be nullified to alert the user that there's no more data.

If you use the detailed knowledge of the element structure, this is straight forward to implement. The disadvantage is that any changes in the structure (say you want to add a member or change one) reflects itself in code changes not isolated to the MODULE. This can be mitigated by providing object-like ``methods'' that hide the details. There is little performance penalty since such routines will in-line with good optimizing compilers. The coding becomes a little more difficult, but the advantages are more robust codes.

Code examples

Slide 13