void * calloc ( size_t num, size_t size );
Allocate array in memory.
Dynamically allocates a block of memory for an array of num elements of size bytes each one. The block is initialized with zeros.
The effective result is the allocation of an initialized memory block of num * size bytes.
A pointer to the allocated space.
The type of this pointer is void*. A type cast to the desired type of data pointer should be performed on this returned pointer in order to be used as an ordinary array of a concrete type.
If the system could not allocate the requested block of memory or if any of the parameters was 0 a NULL pointer is returned.
Defined in ANSI-C.
Some systems may apply restrictions on the maximum size for a memory block.
/* calloc example: rememb-o-matic */
int * pData;
printf ("Enter number of items to be remembered: ");
pData = (int*) calloc (i,sizeof(int));
if (pData==NULL) exit (1);
printf ("Enter number #%d: ",n);
printf ("You have entered: ");
for (n=0;n<i;n++) printf ("%d ",pData[n]);
This program only stores numbers and then prints them out.
But the number of items it can remember is not limited because it allocates
as much dynamic memory as needed to store the number of values that the user
wants to enter.
free, malloc, realloc