calloc.c 714 B

1234567891011121314151617181920212223242526272829303132333435
  1. /* calloc -- allocate memory which has been initialized to zero.
  2. This function is in the public domain. */
  3. /*
  4. @deftypefn Supplemental void* calloc (size_t @var{nelem}, size_t @var{elsize})
  5. Uses @code{malloc} to allocate storage for @var{nelem} objects of
  6. @var{elsize} bytes each, then zeros the memory.
  7. @end deftypefn
  8. */
  9. #include "ansidecl.h"
  10. #include <stddef.h>
  11. /* For systems with larger pointers than ints, this must be declared. */
  12. PTR malloc (size_t);
  13. void bzero (PTR, size_t);
  14. PTR
  15. calloc (size_t nelem, size_t elsize)
  16. {
  17. register PTR ptr;
  18. if (nelem == 0 || elsize == 0)
  19. nelem = elsize = 1;
  20. ptr = malloc (nelem * elsize);
  21. if (ptr) bzero (ptr, nelem * elsize);
  22. return ptr;
  23. }