

calloc()                 General Function                calloc()




Allocate dynamic memory

cchhaarr *ccaalllloocc(_c_o_u_n_t, _s_i_z_e) uunnssiiggnneedd _c_o_u_n_t, _s_i_z_e;

calloc is one of a set  of routines that helps manage a program's
arena.  ccaalllloocc  calls mmaalllloocc  to obtain  a block large  enough to
contain _c_o_u_n_t  items of _s_i_z_e 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 free.

calloc  returns  the  address  of  the  chunk of  memory  it  has
allocated, or NULL if it could not allocate memory.

***** Example *****

This example  attempts to  ccaalllloocc a  small portion of  memory; it
then reallocates it to demonstrate rreeaalllloocc.


#include <stdio.h>

main()
{
        register char *ptr, *ptr2;
        extern char *calloc(), *realloc();
        unsigned count, size;



        count = 4;
        size = sizeof(char *);



        if ((ptr = calloc(count, size)) != NULL)
                printf("%u blocks of size %u calloced\n",
                        count, size);
        else
                printf("Insuff. memory for %u blocks of size %u\n",
                     count, size);



        if ((ptr2 = realloc(ptr,(count*size) + 1)) != NULL)
                printf("1 block of size %u realloced\n",
                        (count*size)+1);
}


***** See Also *****

arena, free(),  general functions, malloc(),  memok(), realloc(),
setbuf()



COHERENT Lexicon                                           Page 1


