Allocate
and
Clear
Memory
calloc
Syntax
#include
<stdlib.h>
void
*ca11oc(nmemb,
size)
size-t
nmemb;
/*
number
of
items
to
allocate
*/
size-t
size;
/*
size
(in
bytes)
of
each
item
*/
Defined
in
memory. c in
rts.
src
Description
The calloc function allocates
size
bytes for each
of
nmemb
objects, and
returns a pointer
to
the space. The function initializes the allocated memory
to all
Os.
If
it
cannot allocate the memory (that
is,
if
it
runs
out
of
memory),
it returns a null pointer
(0).
Example
The memory that calloc
uses
is
in a special memory pool or
heap.
A C
module called memory. c
reserves
memory for the heap in the
.bss
section.
The constant
MEMORY-SIZE
defines the size
of
the heap
as
1000
bytes.
If
necessary, you
can
change the size
of
the heap by change the value
of
MEMORY-SIZE
and reassembling memory. c. For more information,
see
Section 5.1.3, Dynamic Memory Allocation, on page 5-4.
This example
uses
the calloc routine
to
allocate and clear
10
bytes.
ptr
=
calloc(10,2)
;
/*
Allocate
and
clear
10
bytes
*/
6-27