calloc
Allocates main memory.
Defined in header <stdlib.h>
Syntax
#include <stdlib.h>
void *calloc(size_t nitems, size_t size);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ | ■ | ■ |
Remarks
calloc provides access to the C memory heap. The heap is available for dynamic allocation of variable-sized blocks of memory. Many data structures, such as trees and lists, naturally employ heap memory allocation.
All the space between the end of the data segment and the top of the program stack is available for use in the small data models (tiny, small, and medium), except for a small margin immediately before the top of the stack. This margin is intended to allow some room for the application to grow on the stack, plus a small amount needed by DOS.
In the large data models (compact, large, and huge), all space beyond the program stack to the end of physical memory is available for the heap.
calloc allocates a block of size nitems x size. The block is cleared to 0. If you want to allocate a block larger than 64K, you must use farcalloc.
Return value
calloc returns a pointer to the newly allocated block. If not enough space exists for the new block or nitems or size is 0, calloc returns null.
See also
Example
#include <stdio.h>
#include <alloc.h>
#include <string.h>
int main(void)
{
char *str = NULL;
/* allocate memory for string */
str = (char *) calloc(10, sizeof(char));
/* copy "Hello" into string */
strcpy(str, "Hello");
/* display string */
printf("String is %s\n' str)
/* free memory */
free (str);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/stdlib.h/calloc/
· short form /3.1/calloc/