home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / CALLOC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  1.7 KB  |  51 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - calloc.c
  3.  *
  4.  * function(s)
  5.  *        calloc - allocates main memory
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18.  
  19. #include <alloc.h>
  20. #include <stddef.h>
  21. #include <mem.h>
  22.  
  23.  
  24. /*------------------------------------------------------------------------*
  25.  
  26. Name        calloc - allocates main memory
  27.  
  28. Usage        void *calloc(size_t nelem, size_t elsize);
  29.  
  30. Prototype in    stdlib.h and alloc.h
  31.  
  32. Description    calloc allocates a  block like malloc, except the  block is
  33.         of size nelem times elsize. The block is cleared to 0.
  34.  
  35. Return value    calloc returns a  pointer to the newly allocated  block, or
  36.         NULL if not  enough space exists for the new  block or, the
  37.         requested size is equal to 0.
  38.  
  39. *-------------------------------------------------------------------------*/
  40. void *calloc(size_t nelem, size_t elsize)
  41. {
  42.     unsigned long    msize;
  43.     register char    *cp;
  44.  
  45.     msize = (unsigned long)nelem * elsize;
  46.     cp = (msize > 0xFFFF) ? NULL : malloc((unsigned)msize);
  47.     if (cp)
  48.         setmem(cp, (unsigned)msize, 0);
  49.     return(cp);
  50. }
  51.