calloc() — Reserve and Initialize Storage

Format

#include <stdlib.h>
void *calloc(size_t num, size_t size);

Language Level: ANSI

Threadsafe: Yes.

Description

The calloc() function reserves storage space for an array of num elements, each of length size bytes. The calloc() function then gives all the bits of each element an initial value of 0.

Return Value

The calloc() function returns a pointer to the reserved space. The storage space to which the return value points is suitably aligned for storage of any type of object. To get a pointer to a type, use a type cast on the return value. The return value is NULL if there is not enough storage, or if num or size is 0.

Notes:
  1. All heap storage is associated with the activation group of the calling routine. As such, storage should be allocated and deallocated within the same activation group. You cannot allocate heap storage within one activation group and deallocate that storage from a different activation group. For more information about activation groups, see the ILE Concepts manual.
  2. Start of changeTo use teraspace storage instead of single-level store storage without changing the C source code, specify the TERASPACE(*YES *TSIFC) parameter on the compiler command. This maps the calloc() library function to _C_TS_calloc(), its teraspace storage counterpart. The maximum amount of teraspace storage that can be allocated by each call to _C_TS_calloc() is 2GB - 224, or 2147483424 bytes.

    For more information about teraspace storage, see the ILE Concepts manual.

    End of change
  3. Start of changeIf the Quick Pool memory manager has been enabled in the current activation group, then the storage is retrieved using Quick Pool memory manager. See_C_Quickpool_Init() — Initialize Quick Pool Memory Manager for more information.End of change

Example that uses calloc()

This example prompts for the number of array entries required, and then reserves enough space in storage for the entries. If calloc() is successful, the example prints out each entry; otherwise, it prints out an error.

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
  long * array;                            /* start of the array         
   */
  long * index;                            /* index variable             
   */
  int    i;                                /* index variable             
   */
  int  num;                                /* number of entries of the array
*/
 
  printf( "Enter the size of the array\n" );
  scanf( "%i", &num);
 
                                            /* allocate num entries */
  if ( (index = array = (long *) calloc( num, sizeof( long ))) != NULL )
  {
 
    for ( i = 0; i < num; ++i )             /* put values in arr    */
       *index++ = i;                        /* using pointer no     */
 
    for ( i = 0; i < num; ++i )             /* print the array out  */
      printf( "array[%i ] = %i\n", i, array[i] );
  }
  else
  { /* out of storage */
    perror( "Out of storage" );
    abort();
  }
}
/******************  Output should be similar to:  **********************
 
Enter the size of the array
array[ 0 ] = 0
array[ 1 ] = 1
array[ 2 ] = 2
*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]