Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!snorkelwacker!apple!agate!violet.berkeley.edu!jerry From: jerry@violet.berkeley.edu (Jerry Berkman) Newsgroups: comp.lang.fortran Subject: Re: Dimensioning arrays at run-time, best way? Message-ID: <1990Sep22.184727.4648@agate.berkeley.edu> Date: 22 Sep 90 18:47:27 GMT References: <614@keele.keele.ac.uk> <1990Sep17.150155.10220@alchemy.chem.utoronto.ca> Sender: usenet@agate.berkeley.edu (USENET Administrator) Organization: University of California, Berkeley Lines: 42 In article <614@keele.keele.ac.uk> phd11@.uk.ac.keele (Zipzoid) writes: >what is the best way of dimensioning an array whose maximum >memory requirement (size) would be read in at run-time? > On a Cray, you can use automatic arrays: PROGRAM EXAMPLE read *, n call sub1(n) END SUBROUTINE SUB1(N) real X(N) C Do the work in this subroutine. C Of course you can't use common blocks so you'll have to pass X C around as an argument. END Since X's bounds are a dummy argument, the Cray CFT77 compiler generates code to allocate memory for the array when the subroutine is entered, and deallocate it when the subroutine is exited. This is a Fortran 90 feature which is not common, but is probably supported on some other systems. As others have noted, VMS type Fortran systems have an extension that allows passing values, so a similar trick can be used by calling malloc() or something equivalent and passing the value returned as an array. On 4.3 BSD VAX UNIX, there is a routine called falloc(), which stands for Fortran alloc(). This allocates the space but you must use ofsets to a common block to refer to it, less convenient than the above methods. For those new to this, dynamic allocation has been used with Fortran since before I can remember. Almost every big package such as SPSS uses dynamic allocation. I'm glad it will be in Fortran 90. - Jerry Berkman, U.C.Berkeley, (415)642-4804 jerry@violet.berkeley.edu disclaimer: opinions are my own, not my employers, etc.