COHERENT manpages

This page displays the COHERENT manpage for calloc() [Allocate dynamic memory].

List of available manpages
Index


calloc() -- General Function (libc)

Allocate dynamic memory
#include <stdlib.h>
char *calloc(count, size)
unsigned count, size;

The  function calloc()  is one  of a  set of routines  that helps  manage a
program's arena.  calloc() calls malloc() to obtain a block large enough to
contain count  items of size 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  calloc() a  small  portion of  memory; it  then
reallocates it to demonstrate realloc().

#include <stdio.h>
#include <stdlib.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

alloca(),
arena,
free(),
libc,
malloc(),
memok(),
realloc(),
setbuf(),
stdlib.h
ANSI Standard, §7.10.3.1
POSIX Standard, §8.1

Notes

The function alloca() allocates space on the stack.  The space so allocated
does not need to be freed when the function that allocated the space exits.