Allocate an array in memory with elements initialized to 0.
#include <stdlib.h> | For ANSI compatibility (calloc only) | |
#include <malloc.h> | Required only for function declarations |
void *calloc( size_t num, size_t size );
void __based( void ) *_bcalloc( __segment seg, size_t num, size_t size );
void __far *_fcalloc( size_t num, size_t size );
void __near *_ncalloc( size_t num, size_t size );
num | Number of elements | |
size | Length in bytes of each element | |
seg | Segment selector |
The calloc family of functions allocates storage space for an array of num elements, each of length size bytes. Each element is initialized to 0.
In large data models (compact-, large-, and huge-model programs), calloc maps
to _fcalloc. In small data models (tiny-, small-, and medium-model programs),
calloc maps to _ncalloc.
The various calloc functions allocate storage space in the data segments shown in the list below:
Function | Data Segment |
calloc | Depends on data model of program |
_bcalloc | Based heap, specified by seg segment selector |
_fcalloc | Far heap (outside default data segment) |
_ncalloc | Near heap (inside default data segment) |
The calloc functions return 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 _fcalloc and _ncalloc functions return NULL if there is insufficient memory available. The _bcalloc function returns _NULLOFF in this case.
calloc
Standards:ANSI, UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
_bcalloc, _fcalloc, _ncalloc
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
free functions, _halloc, _hfree, malloc functions, realloc functions
/* 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>
void main( void )
{
long *buffer;
buffer = (long *)calloc( 40, sizeof( long ) );
if( buffer != NULL )
printf( "Allocated 40 long integers\n" );
else
printf( "Can't allocate memory\n" );
free( buffer );
}
Allocated 40 long integers