Guardian C Library Calls Reference Manual
calloc
3-14 128833—Guardian TNS C Library Calls Reference Manual
Reference to Library Calls
calloc
The calloc function allocates memory for an array of elements and sets the allocated
memory to zeros.
num_elts
specifies the number of elements in the array.
size_of_elt
specifies the size in bytes of a single element of the array.
Return Value
points to the allocated array, expressed as a pointer to void. If an error occurs, calloc
returns the pointer value NULL.
Usage Guidelines
•
The calloc function allocates memory starting on a word boundary.
•
Note that calloc returns a pointer to void, which is compatible with any pointer type.
However, if you explicitly specify the intended type using a cast expression, your
code will be more maintainable and readable. The following examples show the
intended type using a cast expression.
•
If you have compiled your program for the large-memory model, the calloc function
will automatically enlarge the extended data segment when necessary.
Examples
1. This example allocates memory for an array of 25 elements that are two bytes each:
#include <stdlibh>
char *array_ptr;
size_t nelt;
size_t sizelt;
nelt = 25;
sizelt = 2;
array_ptr = (char *)calloc(nelt, sizelt);
#include <stdlibh>
void *calloc(size_t num_elts, size_t size_of_elt);