home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 1.ddi / CLIBSRC1.ZIP / CALLOC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  1.4 KB  |  49 lines

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