Compaq Fortran for Tru64 UNIX Alpha Systems V5.4

malloc, free, falloc (3f)


Syntax

  integer*4 size
  integer*8 malloc, addr
  external malloc
  addr = malloc ( size )

  integer*4 nelem, elsize, clean, basevac(1), offset
  integer*8 addr
  external falloc
  call falloc ( nelem, elsize, clean, basevec, addr, offset )

  integer*8 addr
  external free
  call free ( addr )

Description

  The malloc, falloc, and free routines provide a general-purpose memory
  allocation package.

  The malloc function returns the address of a block of
  at least size bytes, suitably aligned for any data type (see also malloc(3)).

  Use integer*8 for pointers.

  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 basevac. If basevac is an integer*4 array, elsize should be
  4; if basevac is a real*8 array, elsize should be 8, and so on.  The falloc
  subroutine gets extra bytes so that even if basevac is not naturally
  aligned, all the objects so addressed are within the block.

  The argument to free is the address of a block previously allocated by
  malloc or falloc; 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 basevec(offset+1).

Restrictions

  Results are unpredictable 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 1, 2, and 3 show how to obtain memory and use it within a
  subprogram.

  Example 1:

        integer*8 malloc
        external malloc
        integer work(10)
        pointer (p,work)
        ...
        p = malloc (40)
        do 10 i = 1, n
        work(i) = ...
  10    continue
        ...
        call free (p)

  Example 2 (work is an integer array, elsize is 4):

        integer*8 addr
        integer work(1), offset
        ...
        call falloc (n, sizeof(work(1)), 0, work, addr, offset)
        do 10 i = 1, n
        work(offset+i) = ...
  10    continue
        ...
        call free (addr)

  Example 3 (work is a real*8 array, elsize is 8):

        integer*8 addr
        integer offset
        real*8 work(1)
        ...
        call falloc (n, sizeof(work(1)), 0, work, addr, offset)
        do 10 i = 1, n
        work(offset+i) = ...
  10    continue
        ...
        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*8 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), vsym(lsym), v2(m)
           ...

See Also

  malloc(3)