[comp.sys.apollo] Summary of dynamic memory allocation methods in Fortran

ddawson@csd4.csd.uwm.edu (Daniel R Dawson) (06/21/91)

For your information files...

Good tips for small or new programs that need dynamic memory:
    1) Use the Domain pointer extension in Fortran and use
       the malloc subroutine from the Unix library.
	 Brief example:
	    INTEGER*4 MYPTR, NEXT
	    REAL X, Y, Z
	    POINTER /MYPTR/ X, Y, Z, NEXT
        C
	C Call malloc.
	C
	    CALL MALLOC(16,MYPTR)
	    IF (MYPTR .GT. 0) THEN
        C
        C Now the vars X, Y, Z, NEXT can be filled with data.
	C
	     .
	     .
	     .

    2) Use the falloc (3F) call and store the offset value in COMMON
       before the array.  See the man page for a complete description.
           Brief example:
	      INTEGER*4 DYNOFF,DYNADR,DYNMIC
	      COMMON /MEMORY/ DYNOFF,DYNADR,DYNMIC(1)

              NELEM = 100
	      IELSIZ = 4  (ELEMENT SIZE)
	      CLEAN = 0   (DON'T INITIALIZE MEMORY)

	      CALL FALLOC(NELEM,IELSIZ,CLEAN,DYNMIC,DYNADR,DYNOFF)
	      IF (DYNADR .GT. 0) THEN

              DO 10 I = 1, N
		 DYNMIC(I+DYNOFF) = ...
		 .
		 .
		 .
              CALL FREE(DYNADR)

For large programs (many source files to change) or lazy programmers :
	1) Use the -A sparse_vm linker flag to have the space allocated
	   at run time when the program first accesses the data.  This
	   will prevent huge arrays in programs from grabbing large
	   amounts of disk at program load time.
	      Brief example:
		INTEGER*4 DYNMIC
		COMMON /MEMORY/ DYNMIC(32000000)

            C
	    C Disk space is only taken when DYNMIC(I) is referenced.
	    C
	    C  If the highest subscript used during a run is, say,
	    C  1000, then only 4000 bytes [or 1 (4096 byte) disk block]
	    C  of memory is mapped to disk for the DYNMIC array.
	    C
		 .
		 .
		 .


Thanks again to all who came up with the great ideas!

Special Note To HP Management:
USENET is a much better forum than ADUS in MY opinion.  It's timely,
(all responses came in the first 3 days), inexpensive, accurate, helpful,
non-condescending (usually), and as close as your computer.

-- 
Dan Dawson               | I was seen but not heard. Now I'm only obscene.
ddawson@csd4.csd.uwm.edu | All opinions are only my own!
-------------------------------------------------------------------------
Last month, everything was all Dan's fault.  Damn, I hate that.