MALLOC(3F) UNIX Programmer's Manual MALLOC(3F) NAME malloc, free, falloc - memory allocator SYNOPSIS subroutine malloc (size, addr) integer size, addr subroutine free (addr) integer addr subroutine falloc (nelem, elsize, clean, basevec, addr, offset) integer nelem, elsize, clean, addr, offset DESCRIPTION _M_a_l_l_o_c, _f_a_l_l_o_c and _f_r_e_e provide a general-purpose memory allocation package. _M_a_l_l_o_c returns in _a_d_d_r the address of a block of at least _s_i_z_e bytes beginning on an even-byte boun- dary. _F_a_l_l_o_c allocates space for an array of _n_e_l_e_m elements of size _e_l_s_i_z_e and returns the address of the block in _a_d_d_r. It zeros the block if _c_l_e_a_n is 1. It returns in _o_f_f_s_e_t an index such that the storage may be addressed as _b_a_s_e_v_e_c(_o_f_f_s_e_t+_1) ... _b_a_s_e_v_e_c(_o_f_f_s_e_t+_n_e_l_e_m). _F_a_l_l_o_c gets extra bytes so that after address arithmetic, all the objects so addressed are within the block. The argument to _f_r_e_e is the address of a block previously allocated by _m_a_l_l_o_c or _f_a_l_l_o_c; this space is made available for further allocation, but its contents are left undis- turbed. To free blocks allocated by _f_a_l_l_o_c, use _a_d_d_r in calls to _f_r_e_e, do not use _b_a_s_e_v_e_c(_o_f_f_s_e_t+_1). Needless to say, grave disorder will result if the space assigned by _m_a_l_l_o_cor_f_a_l_l_o_c is overrun or if some random number is handed to _f_r_e_e. DIAGNOSTICS _M_a_l_l_o_c and _f_a_l_l_o_c set _a_d_d_r to 0 if there is no available memory or if the arena has been detectably corrupted by storing outside the bounds of a block. The following example shows how to obtain memory and use it within a subprogram: integer addr, work(1), offset ... call falloc ( n, 4, 0, work, addr, offset ) do 10 i = 1, n work(offset+i) = ... 10 continue Printed 11/26/99 May 15, 1985 1 MALLOC(3F) UNIX Programmer's Manual MALLOC(3F) The next example reads in dimension information, allocates space for two arrays and two vectors, and calls subroutine _d_o_i_t to do the computations: integer addr, dummy(1), offs read *, k, l, m 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, 4, 0, dummy, addr, offs ) call doit( dummy(indm1+offs), dummy(indm2+offs), . dummy(indm3+offs), dummy(indsym+offs), . dummy(indv +offs), m, n, lsym ) 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) ... FILES /usr/lib/libU77.a SEE ALSO malloc(3) Printed 11/26/99 May 15, 1985 2