Path: utzoo!attcan!uunet!zephyr.ens.tek.com!uw-beaver!mit-eddie!snorkelwacker!usc!zaphod.mps.ohio-state.edu!math.lsa.umich.edu!sharkey!msuinfo!news From: fox@DASHER.NSCL.MSU.EDU Newsgroups: comp.lang.fortran Subject: Re: Dimensioning arrays at run-time, best way? Message-ID: <1990Sep18.115704.20642@msuinfo.cl.msu.edu> Date: 18 Sep 90 14:53:24 GMT Sender: news@msuinfo.cl.msu.edu Organization: National Superconducting Cyclotron Lab Lines: 50 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!!!!! In most machines this will cause a very quick failure. No storage has actually been allocated for X. This is because it is passed in as a dummy parameter. FORTRAN expects the storage to have been allocated by the caller which is not the case. What the caller has allocated instead is an argument list with a single entry. The second entry is just garbage and referencing it will at *best* get you garbage, and, on most memory protected machines just cause your program to bus error or access violate depending on the O/S. In subroutine/functions, the dimension of a dummy parameter does *NOT* allocate storage. It simply is a device to tell the compilation unit how to treat the parameter. Storage for the parameter is expected to have been allocated (statically or dynamically depending on the compiler) earlier in the call tree. Ron Ron Fox | FOX@MSUNSCL.BITNET | Where the name NSCL | FOX@CYCVAX.NSCL.MSU.EDU | goes on before Michigan State University | MSUHEP::CYCVAX::FOX | the quality East Lansing, MI 48824-1321 | | goes in. USA