CONTENTS | INDEX | PREV | NEXT
calloc
NAME
calloc - allocate memory and clear
SYNOPSIS
void *ptr = calloc(objsize, numobjs)
size_t objsize;
size_t numobjs;
FUNCTION
Allocate memory and clear. numobjs objects each objsize in size are
allocated contiguously and a pointer to the first object is
returned. The memory is cleared to 0.
Effectively this is equivalent to a malloc(objsize * numobjs); and
then a setmem(ptr, objsize * numobjs, 0); call.
calloc returns NULL if the memory could not be allocated
EXAMPLE
/*
* allocate 16 objects and fill with junk
*/
#include <stdlib.h>
#include <assert.h>
typedef struct {
long a, b, c;
} Junk;
main()
{
Junk *jp;
jp = calloc(sizeof(Junk), 16);
assert(jp);
{
Junk *tj = jp;
short i;
for (i = 0; i < 16; ++i, ++tj) {
tj->a = 1;
tj->b = 2;
tj->c = 3;
}
}
free(jp);
return(0);
}
INPUTS
size_t objsize; size of each object
size_t numobjs; number of objects to allocate
RESULTS
void *ptr; pointer to first object
SEE ALSO
malloc, strdup