BSD 4_3_Tahoe release
[unix-history] / usr / src / man / man3f / malloc.3
CommitLineData
53e9b2ee
KM
1.\" Copyright (c) 1985 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
95f51977 5.\" @(#)malloc.3f 6.1 (Berkeley) 5/15/85
53e9b2ee 6.\"
95f51977 7.TH MALLOC 3F "May 15, 1985"
53e9b2ee
KM
8.UC 6
9.SH NAME
10malloc, free, falloc \- memory allocator
11.SH SYNOPSIS
12.nf
13.B subroutine malloc (size, addr)
14.B integer size, addr
15.PP
16.B subroutine free (addr)
17.B integer addr
18.PP
19.B
20subroutine falloc (nelem, elsize, clean, basevec, addr, offset)
21.B
22integer nelem, elsize, clean, addr, offset
23.fi
24.SH DESCRIPTION
25.IR Malloc , \ falloc
26and
27.I free
28provide a general-purpose memory allocation package.
29.I Malloc
30returns in
31.I addr
32the address of a block of at least
33.I size
34bytes beginning on an even-byte boundary.
35.PP
36.I Falloc
37allocates space for an array of
38.I nelem
39elements of size
40.I elsize
41and returns the address of the block in
42.I addr.
43It zeros the block if
44.I clean
45is 1.
46It returns in
47.I offset
48an index such that the storage may be addressed as
49.I basevec(offset+1) ... basevec(offset+nelem).
50.I Falloc
51gets extra bytes so that after address arithmetic,
52all the objects so addressed are within the block.
53.PP
54The argument to
55.I free
56is the address of a block previously allocated by
57.I malloc
58or
59.IR falloc ;
60this space is made available for further allocation,
61but its contents are left undisturbed.
62To free blocks allocated by
63.I falloc,
64use
65.I addr
66in calls to
67.I free,
68do not use
69.I basevec(offset+1).
70.PP
71Needless to say, grave disorder will result if the space assigned by
72.IR malloc or falloc
73is overrun or if some random number is handed to
74.IR free .
75.SH DIAGNOSTICS
76.I Malloc
77and
78.I falloc
79set
80.I addr
81to 0 if there is no available memory or if the arena
82has been detectably corrupted by storing outside the bounds of a block.
83.PP
84The following example shows how to obtain memory and use it within a
85subprogram:
86.nf
87
88 integer addr, work(1), offset
89 ...
90 call falloc ( n, 4, 0, work, addr, offset )
91 do 10 i = 1, n
92 work(offset+i) = ...
9310 continue
94.fi
95.PP
96The next example reads in dimension information,
97allocates space for two arrays and two vectors,
98and calls subroutine
99.I doit
100to do the computations:
101.nf
102
103 integer addr, dummy(1), offs
104 read *, k, l, m
105 indm1 = 1
106 indm2 = indm1 + k*l
107 indm3 = indm2 + l*m
108 indsym = indm3 + k*m
109 lsym = n*(n+1)/2
110 indv = indsym + lsym
111 indtot = indv + m
112 call falloc ( indtot, 4, 0, dummy, addr, offs )
113 call doit( dummy(indm1+offs), dummy(indm2+offs),
114 . dummy(indm3+offs), dummy(indsym+offs),
115 . dummy(indv +offs), m, n, lsym )
116 end
117 subroutine doit( arr1, arr2, arr3, vsym, vec, m, n, lsym )
118 real arr1(k,l), arr2(l,m), arr3(k,m), vsym(lsym), v2(m)
119 ...
120.fi
121.SH FILES
122/usr/lib/libU77.a
123.SH SEE ALSO
124malloc(3)