F90 Model : Encapsulate Dynamic Memory

LeftRight
Fortran 77 did not have any native way to dynamically allocate memory. As a result several hacks were developed to accomplish much of the same capability, such as:

Fortran 90 includes dynamic memory allocation with a vengeance. It introduces the concept of pointers, automatic arrays, and allocatable arrays; however, be careful! Dynamic memory allocation & deallocation is expensive and it's generally better to avoid repetitious use of it in often called routines.

The following example demonstrates where a data array could be tailored to the problem size, thus minimizing program memory requirements which could be determined at run-time. The allocation is expected to be infrequent, and the data arrays are expected to be shared through out the program.

The module CONTAINS two routines that ``construct'' and ``destruct'' the arrays, as if in an object-oriented language. However, for Fortran 90 these routines will need to be called explicitly since such constructors and destructors are not called automatically. As with C++ an optional argument can be passed to the constructor to pre-set all the array values to a specified value. If no arguments are given, pre-set to zero.

Code examples

LeftRight
Slide 10