falloc(3f)

Name
falloc, free, malloc - allocates memory

Syntax
integer size
integer malloc,addr
external malloc
addr = malloc ( size )

integer nelem, elsize, clean, base(1), addr, offset
external falloc
call falloc ( nelem, elsize, clean, base, addr, offset )

integer addr
external free
call free ( addr )

Description
The falloc, malloc, and free routines provide a general-purpose memory allocation package. The malloc function returns the address of a block of at least size bytes (see also malloc(3) ).

The falloc subroutine allocates space for an array of nelem elements of size elsize and returns the address of the block in addr. The elsize must be one of the following sizes: 1, 2, 4, or 8, and must match the size of the elements of base. If base is an integer*4 array, elsize should be 4; if base is a real*8 array, elsize should be 8, and so on.

The argument to free is the address of a block previously allocated by falloc or malloc; this space is made available for further allocation, but its contents are left undisturbed. To free blocks allocated by falloc, use addr in calls to free; do not use base(offset+1).

Restrictions
Grave disorder will result if the space assigned by malloc or falloc is overrun or if some random number is handed to free.

Diagnostics
Both malloc and falloc set addr to zero if there is no available memory, if the area has been detectably corrupted by storing outside the bounds of a block, or if elsize is invalid.

Examples
Examples 1, 2, and 3 show how to obtain memory and use it within a subprogram.

    Example 1:
          integer  malloc
          external malloc
          integer work(1)
          ...
          p = malloc (10*4)
          ifirst = (p - loc(work(1))/4
          do i = 1, n
             work(i+ifirst) = ...
          enddo
          ...
          call free (p)
    Example 2 (work is an integer array, elsize is 4):
          integer addr
          integer work(1), offset
          ...
          call falloc (n, sizeof(work(1)), 0, work, addr, offset)
          do i = 1, n
             work(offset+i) = ...
          enddo
          ...
          call free (addr)
    Example 3 (work is a real*8 array, elsize is 8):
          integer addr
          integer offset
          real*8 work(1)
          ...
          call falloc (n, sizeof(work(1)), 0, work, addr, offset)
          do i = 1, n
             work(offset+i) = ...
          enddo
          ...
          call free (addr)

Example 4 reads in dimension information, allocates space for two arrays and two vectors, and calls subroutine doit to do the computations.

    Example 4:
          integer addr
          integer dummy(1), offs
          read *, k, l, m, n
          indm1   = 1
          indm2   = indm1 + k*l
          indm3   = indm2 + l*m
          indsym  = indm3 + k*m
          lsym    = n*(n+1)/2
          indv    = indsym + lsym
          indtot  = indv + m
          call falloc (indtot, sizeof(dummy(1)), 0, dummy,
         &             addr, offs)
          call doit (dummy(indm1+offs), dummy(indm2+offs),
         &           dummy(indm3+offs), dummy(indsym+offs),
         &           dummy(indv +offs), m, n, lsym )
          ...
          call free (addr)
          end
             ...
          subroutine doit (arr1, arr2, arr3, vsym, vec, m,
         &                 n, lsym)
          real arr1(k,l), arr2(l,m), arr3(k,m)
          real vsym(lsym), v2(m)
             ...

Files
libU77.a

Bugs
The memory address pointers addr should be integer*8 on 64 bit machines, such as DEC Alpha's and the SGI machines running IRIX64. On machines with a 32 bit memory the address pointers addr should be integer*4.

See Also
loc(3), malloc(3)