Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Allocates an array in memory with elements initialized to 0.

void *calloc( 
   size_t num,
   size_t size 


Number of elements.
Length in bytes of each element.

Return Value

calloc returns a pointer to the allocated space. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.


The calloc function allocates storage space for an array of num elements, each of length size bytes. Each element is initialized to 0.

calloc calls malloc to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when calloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call


early in your program, or link with NEWMODE.OBJ.

When the application is linked with a debug version of the C run-time libraries, calloc resolves to _calloc_dbg. For more information about how the heap is managed during the debugging process, see The CRT Debug Heap.


Routine Required header Compatibility
calloc <stdlib.h> and <malloc.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_calloc.c
/* This program uses calloc to allocate space for
 * 40 long integers. It initializes each element to zero.
#include <stdio.h>
#include <malloc.h>

int main( void )
   long *buffer;

   buffer = (long *)calloc( 40, sizeof( long ) );
   if( buffer != NULL )
      printf( "Allocated 40 long integers\n" );
      printf( "Can't allocate memory\n" );
   free( buffer );


Allocated 40 long integers

See Also

Memory Allocation Routines | free | malloc | realloc | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft