calloc -- Allocate and Clear Memory

SYNOPSIS

 #include <stdlib.h>

 void *calloc(size_t n, size_t size);
 

DESCRIPTION

calloc allocates a block of dynamic memory to contain n elements of the size specified by size. The block is cleared to binary 0s before return.

RETURN VALUE

calloc returns the address of the first character of the new block of memory. The allocated block is suitably aligned for storage of any type of data.

ERRORS

User ABEND 1205 or 1206 may occur if memory management data areas are overlaid.

DIAGNOSTICS

If adequate memory is not available or if you request 0 bytes, NULL is returned.

IMPLEMENTATION

The size of the block allocated is at least n * size.

Under an XA or ESA operating system, memory allocated by calloc can reside above the 16-megabyte line for programs that run in 31-bit addressing mode.

See malloc for further implementation information.

EXAMPLE

This function allocates a square identity matrix (one in which all elements are zeroes except for those on the diagonal). The number of rows and columns is passed as an argument. If memory cannot be allocated, a NULL pointer is returned.
  #include <stdio.h>
  #include <stdlib.h>

  double *identity(int size) {
     double *matrix;
     int i;

     matrix = calloc(sizeof(double), size*size);
     if (matrix == NULL) return(NULL);
     for (i = 0; i < size; ++i)
        matrix[size*i + i] = 1.0;
     return matrix;
  }

 

RELATED FUNCTIONS

malloc

SEE ALSO

Memory Allocation Functions

Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.