Path: utzoo!attcan!telly!lethe!yunexus!ists!helios.physics.utoronto.ca!alchemy.chem.utoronto.ca!mroussel From: mroussel@alchemy.chem.utoronto.ca (Marc Roussel) Newsgroups: comp.lang.fortran Subject: Re: Dimensioning arrays at run-time, best way? Message-ID: <1990Sep20.211813.5333@alchemy.chem.utoronto.ca> Date: 20 Sep 90 21:18:13 GMT References: <1990Sep18.115704.20642@msuinfo.cl.msu.edu> Organization: Department of Chemistry, University of Toronto Lines: 54 In article <1990Sep18.115704.20642@msuinfo.cl.msu.edu> fox@DASHER.NSCL.MSU.EDU writes: >In article <1990Sep17.150155.10220@alchemy.chem.utoronto.ca>, mroussel@alchemy.chem.utoronto.ca (Marc Roussel) writes... > >>In article <614@keele.keele.ac.uk> phd11@.uk.ac.keele (Zipzoid) writes: >>>I think this question may have been asked many times before, >>>but what is the best way of dimensioning an array whose maximum >>>memory requirement (size) would be read in at run-time? >> >>Let your PROGRAM block be a dummy. (I.E. don't put any code into it >>that does anything but set N (the array size) and calls a subroutine >>which does the real work.) Then call your subroutine. Most FORTRAN >>compilers don't check for the right number of parameters in a subroutine >>call, so the following may not be portable, but it should do the work on >>most computers. >> >>Now for the example: >> >> PROGRAM EXAMPLE >> read(*,*)n >> call sub1(n) >> END >> >> SUBROUTINE SUB1(N,X) >> 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 > >NO NO NO A thousand times NO!!!!! >[Lots of good reasons why this is dangerous deleted.] In my own defence, I've used this sort of trick before without ill effects. It's not standard-conforming, portable or even safe to assume that your machine will stay up after you've executed a program based on the above model. But on some machines with some compilers it may work. Maybe I should have included a disclaimer with the code. :-) In a more serious vein, if you are working on a personal machine with a straightforward architecture, you can compile the above code and then hack at the generated machine language. Another alternative would be to put an explicitly declare the size in the main program but then to compile the main program and subroutines separately. You would then only have to recompile the main program (and relink) when you wanted to change the size(s) of your array(s). If you do all the real work in subroutines, recompiling the main program shouldn't generally take very long, and this should work just about anywhere (unlike my... uhh... unique previous solution). Marc R. Roussel mroussel@alchemy.chem.utoronto.ca