[Previous] [Contents] [Next]

calloc()

Allocate space for an array

Synopsis:

#include <malloc.h>

void* calloc ( size_t n, 
               size_t size );

Library:

libc

Description:

The calloc() function allocates space from the heap for an array of n objects, each of size bytes, and initializes them to 0.


Note: A block of memory allocated with the calloc() function should be freed using the free() function.

Returns:

A pointer to the start of the allocated memory or NULL if there's insufficient memory available or if size is zero.

Examples:

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
    char* buffer;

    buffer = (char* )calloc( 80, sizeof(char) );
    if( buffer == NULL ) {
        printf( "Can't allocate memory for buffer!\n" );
        return EXIT_FAILURE;
    }

    free( buffer );

    return EXIT_SUCCESS;
}

Classification:

ANSI

Safety:
Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes

See also:

free(), malloc(), realloc(), sbrk()


[Previous] [Contents] [Next]