Subject: memory allocation in Fortran (was Re: HEASARC Continuation Keyword

From: Lucio Chiappetti <lucio at ifctr.mi.cnr.it>
Date: Thu, 27 May 1993 09:32:50 +0200 (MET DST)

On Wed, 26 May 1993, Chris Flatters wrote:

> Remember that many FITS readers are FORTRAN-based for historical
> reasons and there is no way to dynamically allocate or reallocate an
> array or character string in FORTRAN 77.
> 

  Unless you write some Fortran-callable C wrapper around some routine
  like calloc. I've done this both for VMS and Unix (the two routines are
  almost identical), and I'm using this routinely now.

  From Fortran you do something like :

  e.g. REAL ARRAY(1)
  ...
  CALL Z_ALLOC(10000,4,ARRAY,IADDR,IOFF)
  
  and this virtually "extends" ARRAY to 10000 elements. To access the
  n-th element of the array you do ARRAY(n+IOFF). To pass the array to
  a subroutine you just CALL SUB(ARRAY(1+IOFF)), and in SUBROUTINE SUB(A)
  you just declare A(*) and go on as usually.

  This preserves as far possible (or does not disrupt too much) the 
  traditional Fortran legibility of the programs (this is for those like
  me which like to go on working with Fortran ... I've isolated all the
  few system dependent stuff in some 13 C routines (Unix), or 2 C routines
  (VMS, where I use C only for Z_ALLOC and its converse) and use happily
  Fortran for all the rest.

  I am using happily the above memory allocation, I find it useful for
  cases like when a program has to read a large array (e.g. an image, or
  an entire file header as a large character string) of size unspecified
  at compile time but a priori known just after I open the file.
  I never tried nor want to do re-allocation, or using continuosly dynamic
  allocation for small chunks of strings, the way (some) C programmers do.
  
  I am not at all a C expert, nor I am fond of C, and the following text 
  in the Ultrix alloc man page (which I am not sure to understand
  fully) actually scared me and discouraged even to try that.

     Currently, the allocator is unsuitable for direct use in a large virtual
     environment where many small blocks are kept, since it keeps all allo-
     cated and freed blocks on a circular list.  Just before more memory is
     allocated, all allocated and freed blocks are referenced.

-----------------------------------------------------------------------------   
       A member of  G.ASS : Group for Astronomical Software Support             
-----------------------------------------------------------------------------   
Lucio Chiappetti - IFCTR/CNR Milano        |         U N I C U I Q U E          
via Bassini 15 - I-20133 Milano - Italy    |          System  Manager           
Internet: LUCIO at IFCTR.MI.CNR.IT            |           FORTUNAE SUAE            
Decnet:   IFCTR::LUCIO (39610::LUCIO)      |                                    
Bitnet:   <please do not use any more>     |                                    
-----------------------------------------------------------------------------