Header File
alloc.h, stdlib.h
Category
Memory Routines
Prototype
void *calloc(size_t nitems, size_t size);
Description
Allocates main memory.
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.
calloc allocates a block of size nitems * size. The block is initialized to 0.
Return Value
calloc returns a pointer to the newly allocated block. If not enough space exists for the new block or if nitems or size is 0, calloc returns NULL.
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; }
Portability
POSIX |
Win32 |
ANSI C |
ANSI C++ |
+ |
+ |
+ |
+ |
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|