.TH calloc() "" "" "General Function (libc)"
.PC "Allocate dynamic memory"
.B "#include <stdlib.h>"
\fBchar *calloc(\fIcount\^\fB, \fIsize\^\fB)\fR
\fBunsigned \fIcount\^\fB, \fIsize\^\fB;\fR
.PP
The function
.B calloc()
is one of a set of routines that helps manage a program's arena.
\fBcalloc()\fR calls \fBmalloc()\fR to obtain a block large enough to contain
\fIcount\fR items of \fIsize\fR bytes each; it then
initializes the block to zeroes.
When this memory is no longer needed, you can return it to the free
pool by using the function
.BR free() .
.PP
.B calloc()
returns the address of the chunk of memory it has allocated, or
NULL if it could not allocate memory.
.SH Example
This example attempts to \fBcalloc()\fR a small portion of memory; it
then reallocates it to demonstrate \fBrealloc()\fR.
.DM
#include <stdio.h>
#include <stdlib.h>
.DE
.DM
main()
{
	register char *ptr, *ptr2;
	extern char *calloc(), *realloc();
	unsigned count, size;
.DE
.DM
	count = 4;
	size = sizeof(char *);
.DE
.DM
	if ((ptr = calloc(count, size)) != NULL)
		printf("%u blocks of size %u calloced\en",
		        count, size);
	else
		printf("Insuff. memory for %u blocks of size %u\en",
		     count, size);
.DE
.DM
	if ((ptr2 = realloc(ptr,(count*size) + 1)) != NULL)
		printf("1 block of size %u realloced\en",
			(count*size)+1);
}
.DE
.SH "See Also"
.Xr alloca(), alloca
.Xr arena, arena
.Xr free(), free
.Xr libc, libc
.Xr malloc(), malloc
.Xr memok(), memok
.Xr realloc(), realloc
.Xr setbuf(), setbuf
.Xr stdlib.h stdlib.h
.br
\*(AS, \(sc7.10.3.1
.br
\*(PX Standard, \(sc8.1
.SH Notes
The function
.B alloca()
allocates space on the stack.
The space so allocated does not need to be freed when the function that
allocated the space exits.
